Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8036245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:33:24+00:00 2026-06-05T02:33:24+00:00

I tried some date functions, because I would like to get some dates of

  • 0

I tried some date functions, because I would like to get some dates of the next month with PHP, but I experienced some problems.

The current date as a MySQL-compatible timestamp (date('Y-m-d H:i:s');) is the following:
'2012-05-31 14:59:19'

  • date('t', strtotime('next month')); which gives the number of days in the next month, returns 31 as a result… which is not correct, because June consists of only 30 days (seems like it outputs the days of July).
  • date('Y-m-d H:i:s', strtotime('next month'));, which gives the next month’s timestamp format, returns '2012-07-01 14:59:19', but I would expect the following:
    '2012-06-30 14:59:19'.

So I tried some codes, here are my experiments, using date(), strtotime() and DateTime class: http://pastebin.com/3iaH4iSZ

And the OUTPUT looks like the following (you can also see it here!):
PHP date functions related to NEXT month

Are these PHP bugs, or do I misunderstand something?


I would expect the same results as when using MySQL’s date functions, just a few examples:

SELECT NOW( )

2012-05-31 17:51:15

SELECT DATE_ADD(NOW(), INTERVAL 1 MONTH)

2012-06-30 17:51:15

SELECT DATE_ADD( '2013-01-30 17:51:15', INTERVAL 1 MONTH )

2013-02-28 17:51:15

SELECT DATE_ADD( '2013-01-31 17:51:15', INTERVAL 1 MONTH )

2013-02-28 17:51:15

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:33:25+00:00Added an answer on June 5, 2026 at 2:33 am

    OK, I think I found the solution, which behaves similar to MySQL’s DATE_ADD function with INTERNAL 1 MONTH.
    I tested my function with all the possibly problematic cases, and it seems to work correctly.
    I paste it here, maybe someone will find it useful. I put some comments in this code, so I think it’s clear enough.

    The function is called getOneMonthLaterTimestamp. An example:

    echo getOneMonthLaterTimestamp('2012-05-31 17:51:15');
    

    Output: 2012-06-30 17:51:15

    The function

    /**
     * Test function for dumping variables in a readable way
     * 
     * @param mixed $stuff
     * @param string $text
     * @return string 
     */
    function my_var_export($stuff, $text = '...') {
        return '<p>' . $text . ' (' . gettype($stuff) . '):</p><pre>' . var_export($stuff, TRUE) . '</pre>';
    }
    
    /**
     * Get timestamp format of the date one month later 
     * than the date given in the argument/current date if left empty.
     * 
     * Behaves similar to MySQL's DATE_ADD function with INTERVAL 1 MONTH:
     * SELECT DATE_ADD('2012-05-31 17:51:15', INTERVAL 1 MONTH)
     * --> 2012-06-30 17:51:15
     * SELECT DATE_ADD('2013-01-30 17:51:15', INTERVAL 1 MONTH )
     * --> 2013-02-28 17:51:15
     * 
     * these are equivalent to:
     * echo getOneMonthLaterTimestamp('2012-05-31 17:51:15');
     * echo getOneMonthLaterTimestamp('2013-01-30 17:51:15');
     * 
     * You can also call it without an argument. This way, the current date is taken as a basis.
     * echo getOneMonthLaterTimestamp();
     * 
     * @param string $DateTime_param date/time string
     * @see http://www.php.net/manual/en/datetime.formats.php
     * @see http://www.php.net/manual/en/datetime.construct.php
     * 
     * @return string Date one month later as a MySQL-compatible timestamp format
     */
    function getOneMonthLaterTimestamp($DateTime_param = NULL) {
        // if argument is left empty, the current date is taken as a basis
        if (empty($DateTime_param)) {
            $DateTime_param = date('Y-m-d H:i:s');
        }
    
        $lastDayOfNextMonth = new DateTime($DateTime_param);
        $lastDayOfNextMonth->modify('last day of next month');
    
        $nextMonth = new DateTime($DateTime_param);
        $nextMonth->modify('next month');
    
        if ($lastDayOfNextMonth->format('n') < $nextMonth->format('n')) {
            $oneMonthLaterTimestamp = $lastDayOfNextMonth->format('Y-m-d H:i:s');
        }
        else {
            $oneMonthLaterTimestamp = $nextMonth->format('Y-m-d H:i:s');
        }
    
        return $oneMonthLaterTimestamp;
    }
    

    Test cases

    $timestamps_to_test_array = array(
      date('Y-m-d H:i:s'),   // 1. current date
      '2011-01-28 23:59:59', // 2.
      '2011-01-29 23:59:59', // 3.
      '2011-01-30 23:59:59', // 4.
      '2011-01-31 23:59:59', // 5.
      '2012-01-28 23:59:59', // 6.
      '2012-01-29 23:59:59', // 7.
      '2012-01-30 23:59:59', // 8.
      '2012-01-31 23:59:59', // 9.
      '2012-02-29 23:59:59', // 10.
      '2012-03-30 23:59:59', // 11.
      '2012-03-31 23:59:59', // 12.
      '2012-04-30 23:59:59', // 13.
      '2012-05-31 23:59:59', // 14.
      '2012-12-31 23:59:59', // 15.
      '2013-01-31 23:59:59', // 16.
      '2013-02-28 23:59:59', // 17.
    );
    
    $i = 1;
    
    foreach ($timestamps_to_test_array as $timestamp_to_test) {
        $oneMonthLaterTimestamp = getOneMonthLaterTimestamp($timestamp_to_test);
        echo my_var_export($timestamp_to_test, '[' . $i . ']. Timestamp to test ($timestamp_to_test)');
        echo my_var_export($oneMonthLaterTimestamp, 'Timestamp + 1 month (getOneMonthLaterTimestamp($timestamp_to_test))');
        echo '<hr />';
        $i++;
    }
    

    Output of the test cases

    I pasted the output here: http://pastebin.com/rY5ZRBs9.
    Here’s a screenshot of it: https://i.stack.imgur.com/9q23l.png

    Getting the number of days in the next month

    /**
     * Get number of days in the next month
     *
     * @param string $DateTime_param date/time string
     * @return int Number of days in the next month
     */
    function getNumberOfDaysInNextMonth($DateTime_param = NULL) {
        // if argument is empty, the current date is taken as a basis
        if (empty($DateTime_param)) {
            $DateTime_param = date('Y-m-d H:i:s');
        }
    
        // DateTime instance
        $dateCurrent = new DateTime($DateTime_param);
        $dateCurrent->modify('last day of next month');
        return (int)$dateCurrent->format('t');
    }
    
    /**
     * It's identical to getNumberOfDaysInNextMonth()
     *
     * @see getNumberOfDaysInNextMonth
     */
    function getLastDayOfNextMonth($DateTime_param = NULL) {
        return getNumberOfDaysInNextMonth($DateTime_param);
    }
    

    Test case

    $lastDayOfNextMonth = getLastDayOfNextMonth('2012-05-31 03:50:27');
    echo my_var_export($lastDayOfNextMonth, "Get last day of next month (getLastDayOfNextMonth('2012-05-31 03:50:27'))");
    

    Output:

    Get last day of next month (getLastDayOfNextMonth('2012-05-31 03:50:27')) (integer):
    30
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried using date(m/d/Y, strtotime(04-05-2012)) but I will get 05/04/2012 or on some
I'm experiencing some problems when using the default date:difference EXSLT template, provided at http://www.exslt.org/date/functions/difference/index.html
I need to get the end date of the given month for some calculation
I tried some add to favorties JavaScript scripts.. With IE8, I get an access
I tried some codes but after it is send to mail account, it is
I'm guessing there is a simple solution here, but I've tried some things and
I tried following some of the code from here and here , but I
I've tried quite some fixes i found on stackoverflow and elsewhere but I couldn't
I am having some problems with hibernate and MySQL. I've been reading but I
Just tried some small graphics application of mine on Windows 7, and I'm getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.