开发者

MySQL's DATE_ADD function in PHP

I need to implement the MySQL DATE_ADD functionality in PHP, so I have taken the function provided on the official PHP website at the "strtotime" documentation page like follows:

function get_x_months_to_the_future( $base_time = null, $months = 1 )
{
    if (is_null($base_time))
        $base_time = time();

    $x_months_to_the_future    = strtotime( "+" . $months . " months", $base_time );

    $month_before              = (int) date( "m", $base_time ) + 12 * (int) date( "Y", $base_time );
    $mon开发者_Go百科th_after               = (int) date( "m", $x_months_to_the_future ) + 12 * (int) date( "Y", $x_months_to_the_future );

    if ($month_after > $months + $month_before)
        $x_months_to_the_future = strtotime( date("Ym01His", $x_months_to_the_future) . " -1 day" );

    return $x_months_to_the_future;
} //get_x_months_to_the_future()

It works perfectly, but I don't understand why in the $month_before and $month_after calculation the year is multiplied by 12 in both cases. What difference does that make and is that really needed? I simply don't know why and would like to understand that code completely.


I think I just realized why... because it converts the years to months and then adds the other months... d'oh! :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜