Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accurate method to add months in PHP 5.1?

Tags:

php

strtotime

Yesterday I ran into an issue with PHP's strtotime not properly adding a month. On '2011-05-31' I ran:

date('Y-m-d',strtotime( '+1 month', strtotime('now')));

Which returns '2011-07-01' when I am expecting '2011-06-30'.

MySQL doesn't have any issue doing this.

I'd rather not reinvent the wheel with this, as it is fairly easy to make mistakes with date calculations from my experience.

Does anyone have a reliable and tested solution for this for PHP 5.1?

like image 860
Sgraffite Avatar asked Jun 01 '11 20:06

Sgraffite


People also ask

How to add months to a date in PHP?

To add months to a date in php, you can use the strtotime() function. You can also use the php date_add() function. Skip to primary navigation Skip to main content Skip to primary sidebar The Programming Expert Solving All of Your Programming Headaches HTML JavaScript jQuery PHP Python SAS VBA About You are here: Home/PHP/Add Months to Date in php

How do I adjust the year in PHP?

1. function adjustYear (int $yearsAdj) { //you can pass in +/- value and I adjust year value by that value but then I also call PHP's 'cal_days_in_month' function to ensure the day number I have in my date does not exceed days in the month for the new year/month combo--if it does, I adjust the day value downward.

How to retrieve month partition from current date in PHP?

In the following, you’ll learn methods to retrieve month portion from current date or from any date– Method 1: Using date() function to retrieve current month. PHP’s date() function can let you know date and time related information based on the formatting characters it takes in its first parameter. The function can take maximum of two parameters.

How to retrieve current month using date in MySQL?

Method 1: Using date () function to retrieve current month 1 m- To represent a month as a number with leading zero. Ex. 01, 12 2 M – To represent a month as short text of three letters. Ex. Jun 3 n – To represent a month as a number without leading zero. Ex. 1, 12


2 Answers

It certainly is possible in PHP: Check the strtotime manual, especially this comment.

If you have a MySQL connection available, SELECT DATE_ADD( '2011-05-31', INTERVAL 1 MONTH ) would be less redundant since the (correct) functionality is already implemented without you having to implement it yourself.

like image 70
mabako Avatar answered Oct 12 '22 21:10

mabako


Since this seems to be a fairly confusing topic here is some information on it:

You are actually getting an accurate result it is literally increasing the month by 1, the day remains 31, therefore the date is 2011-06-31. If you do echo date('Y-m-d', strtotime('2011-06-31')); you'll see it displays 2011-07-01.

Here is one method of making this work as "expected" in PHP 5.1 (and before)

function next_month($timestamp)
{
    $next_month = date('m', $timestamp);
    $next_month++;

    $next_year = date('Y', $timestamp);

    if($next_month == 12)
    {
        $next_year++;
    }

    if(date('d', $timestamp) <= date('t', mktime(0, 0, 0, $next_month, 1, $next_year)))
    {
        return date('Y-m-d',strtotime( '+1 month', $timestamp));
    }
    else
    {
        return date('Y-m-d', mktime(0, 0, 0, $next_month, date('t', mktime(0, 0, 0, $next_month, 1, $next_year)), $next_year));
    }
}

echo next_month(strtotime('2011-05-31'));

echo next_month(strtotime('2011-05-01'));

This is modified code from a library I wrote a while ago -- I've never found an elegant solution.

For PHP 5.3+

Refer to PHP DateTime::modify adding and subtracting months for a detailed Q/A on this topic.

like image 41
tplaner Avatar answered Oct 12 '22 21:10

tplaner