PHP将日期加1个月

6

我有一个函数,返回一个月前的URL。

我想显示当前选择的月份,但我不能使用简单的当前月份,因为当用户点击链接回到1个月前时,选择的月份会改变并且不再是当前月份。

因此,该函数返回2012年8月。

我该如何编写一个小的PHP脚本来增加1个月?

目前为止,我已经:

<?php echo strip_tags(tribe_get_previous_month_text()); ?>

可能是PHP Strtotime -1month -2month的重复问题。 - Fakhruddin Ujjainwala
7个回答

10

简单方法:

$next_month = strtotime('august 2012 next month');
更好的方法:
$d = new Date('August 2012');
$next_month = $d->add(new DateInterval('P1M'));

相关文档:strtotime date dateinterval


第二种方法有什么优点? - Jeremy Holovacs
strtotime只提供原始时间戳。而复杂的日期/时间对象则可以让您轻松进行任何其他类型的操作,包括更多的日期“数学”运算,同时随时获取原始时间戳。 - Marc B
你希望你的 PHP 版本是否大于等于 5.2.0? - Glavić
1
你不能直接输出 DateTime 对象,必须使用 ->format('d.m.Y') 来格式化输出... - Glavić
如果您的 PHP 版本大于等于 5.4,可以将其放在一行中,如下所示:$d = (new DateTime('August 2012'))->add(new DateInterval('P1M'))->format('F Y'); - Glavić
显示剩余6条评论

7

有三个选项/答案

     $givendate is the given date (ex. 2016-01-20)

option 1:
        $date1 = date('Y-m-d', strtotime($givendate. ' + 1 month'));

option 2:
        $date2 = date('Y-m-d', strtotime($givendate. ' + 30 days'));

option 3:
        $number = cal_days_in_month(CAL_GREGORIAN, date('m', strtotime($givendate)), date('Y', strtotime($givendate)));
        $date3 = date('Y-m-d', strtotime($date2. ' + '.$number.' days'));

2

嗨,除了他们的回答外。我认为如果你只想根据当前日期获取下个月,这是我的解决方案。

$today = date("Y-m-01");

$sNextMonth = (int)date("m",strtotime($today." +1 months") );

请注意,我不断地将日期定义为01,以便我们在取得下一个月时更加安全。如果是date("Y-m-d");且当前日期为31,则会失败。
希望这可以帮到你。

2

2
你可以使用DateTime类和DateTime::add()方法来实现:

文档


tribe_get_previous_month_text()输出一个字符串。它会正常工作吗? - user796443
你需要将它放在构造函数中。请参阅http://fr2.php.net/manual/en/datetime.construct.php。 - Lucas Holt
对我来说非常好用,谢谢!我需要今天 + 1年。 $date = new DateTime(); $date->add(new DateInterval('P1Y')); - Nick Constantine

0

日期差异

$date1 = '2017-01-20';
$date2 = '2019-01-20';

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);

$month1 = date('m', $ts1);
$month2 = date('m', $ts2);

echo $joining_months = (($year2 - $year1) * 12) + ($month2 - $month1);

-1

由于我们知道strtotime(+1个月)总是添加30天,因此对于以31日,30日或29日结尾的日期可能会有一些麻烦,如果您仍然希望保持在下个月的最后一天内。

因此,我编写了这个过于复杂的脚本来解决这个问题,并使其适应所有类型的格式,如年,月,日,小时,分钟和秒的增加。


function seetime($datetime, $p = '+', $i, $m = 'M', $f = 'Y-m-d H:i:s')
{
    
    /* 
    $datetime needs to be in format of YYYY-MM-DD HH:II:SS but hours, minutes and seconds are not required
    $p can only be "+" to increse or "-" to decrese
    $i is the amount you want to change
    $m is the type you want to change
    
    Allowed types:
    Y = Year
    M = Months
    D = Days
    W = Weeks
    H = Hours
    I = Minutes
    S = Seconds
    
    $f is the datetime format you want the result to be returned in
    
    */
    $validator_y = substr($datetime,0,4);
    $validator_m = substr($datetime,5,2);
    $validator_d = substr($datetime,8,2);
    
    if(checkdate($validator_m, $validator_d, $validator_y))
    {
        $datetime = date('Y-m-d H:i:s', strtotime($datetime));
        #$p = either "+" to add or "-" to subtract
        if($p == '+' || $p == '-')
        {
            if(is_int($i))
            {
                if($m == 'Y')
                {
                    $year = date('Y', strtotime($datetime));
                    $rest = date('m-d H:i:s', strtotime($datetime));
                    
                    if($p == '+')
                    {
                        $ret = $year + $i;
                    }
                    else
                    {
                        $ret = $year - $i;
                    }
                    
                    $str = $ret.'-'.$rest;
                    
                    return(date($f, strtotime($str)));
                }
                elseif($m == 'M')
                {
                    $year = date('Y', strtotime($datetime));
                    $month = date('n', strtotime($datetime));
                    $rest = date('d H:i:s', strtotime($datetime));
                    $his = date('H:i:s', strtotime($datetime));
                    if($p == '+')
                    {
                        $ret = $month + $i;
                        $ret = sprintf("%02d",$ret);
                        
                    }
                    else
                    {
                        $ret = $month - $i;
                        $ret = sprintf("%02d",$ret);
                        
                    }
                    
                    
                    if($ret < 1)
                    {
                        $ret = $ret - $ret - $ret;
                        $years_back = floor(($ret + 12) / 12);
                        $monts_back = $ret % 12;
                        $year = $year - $years_back;
                        $month = 12 - $monts_back;
                        $month = sprintf("%02d",$month);
                        $new_date = $year.'-'.$month.'-'.$rest;
                        $ym = $year.'-'.$month;
                        
                        $validator_y = substr($new_date,0,4);
                        $validator_m = substr($new_date,5,2);
                        $validator_d = substr($new_date,8,2);
                        if(checkdate($validator_m, $validator_d, $validator_y))
                        {
                            return (date($f, strtotime($new_date)));
                        }
                        else
                        {
                            $days = date('t',strtotime($ym));
                            $new_date = $ym.'-'.$days.' '.$his;
                            return (date($f, strtotime($new_date)));
                        }
                    }
                    if($ret > 12)
                    {
                        
                        $years_forw = floor($ret / 12);
                        $monts_forw = $ret % 12;
                        $year = $year + $years_forw;
                        $month = sprintf("%02d",$monts_forw);
                        $new_date = $year.'-'.$month.'-'.$rest;
                        $ym = $year.'-'.$month;
                        
                        $validator_y = substr($new_date,0,4);
                        $validator_m = substr($new_date,5,2);
                        $validator_d = substr($new_date,8,2);
                        if(checkdate($validator_m, $validator_d, $validator_y))
                        {
                            return (date($f, strtotime($new_date)));
                        }
                        else
                        {
                            $days = date('t',strtotime($ym));
                            $new_date = $ym.'-'.$days.' '.$his;
                            return (date($f, strtotime($new_date)));
                        }
                    }
                    else
                    {
                        $ym = $year.'-'.$month;
                        $new_date = $year.'-'.$ret.'-'.$rest;
                        
                        $validator_y = substr($new_date,0,4);
                        $validator_m = substr($new_date,5,2);
                        $validator_d = substr($new_date,8,2);
                        if(checkdate($validator_m, $validator_d, $validator_y))
                        {
                            return (date($f, strtotime($new_date)));
                        }
                        else
                        {
                            $ym = $validator_y . '-'.$validator_m;
                            $days = date('t',strtotime($ym));
                            $new_date = $ym.'-'.$days.' '.$his;
                            return (date($f, strtotime($new_date)));
                        }
                        
                    }
                }
                elseif($m == 'D')
                {
                    return (date($f, strtotime($datetime.' '.$p.$i.' days')));
                }
                elseif($m == 'W')
                {
                    return (date($f, strtotime($datetime.' '.$p.$i.' weeks')));
                }
                elseif($m == 'H')
                {
                    return (date($f, strtotime($datetime.' '.$p.$i.' hours')));
                }
                elseif($m == 'I')
                {
                    
                    return (date($f, strtotime($datetime.' '.$p.$i.' minutes')));
                }
                elseif($m == 'S')
                {
                    return (date($f, strtotime($datetime.' '.$p.$i.' seconds')));
                }
                else
                {
                    return 'Fourth parameter can only be any of following: Valid Time Parameters Are: Y M D Q H I S';
                }
            }
            else
            {
                return 'Third parameter can only be a number (whole number)';
            }           
        }
        else
        {
            return 'Second parameter can only be + to add or - to subtract';
        }
    }
    else    
    {
        return 'Date is not a valid date';
    }   
    
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接