SHOW:
|
|
- or go back to the newest paste.
1 | - | <?php |
1 | + | |
2 | // PHP: date functions' results related to the NEXT month are not correct | |
3 | // http://stackoverflow.com/questions/10834350/php-date-functions-results-related-to-the-next-month-are-not-correct-php-bugs | |
4 | ||
5 | // RESULTS (OUTPUT) of http://pastebin.com/3iaH4iSZ | |
6 | - | // testing function for printing out variables |
6 | + | // @see http://i.stack.imgur.com/mHHRu.png |
7 | - | function my_var_export($stuff, $text = '...'){ |
7 | + | |
8 | - | return '<p>'.$text.':</p><pre>'.var_export($stuff, TRUE).'</pre>'; |
8 | + | strtotime('today');: |
9 | - | } |
9 | + | 1338415200 |
10 | ||
11 | - | // Current date as a MySQL-compatible timestamp |
11 | + | strtotime('next month');: |
12 | - | $current_date_timestamp = date('Y-m-d H:i:s'); |
12 | + | 1341147559 |
13 | - | |
13 | + | |
14 | - | // Get next month as a timestamp |
14 | + | Current date as a MySQL-compatible timestamp [ date('Y-m-d H:i:s'); ]: |
15 | - | $next_month_timestamp = date('Y-m-d H:i:s', strtotime('next month')); |
15 | + | '2012-05-31 14:59:19' |
16 | - | |
16 | + | |
17 | - | // Get last day of the current month/Get number of days in the current month |
17 | + | Get next month as a timestamp [ date('Y-m-d H:i:s', strtotime('next month')); ]: |
18 | - | $last_day_current_month = date('t', strtotime('today')); |
18 | + | '2012-07-01 14:59:19' |
19 | ||
20 | - | // Get last day of the next month/Get number of days in the next month |
20 | + | Get last day of the next month as a timestamp with DateTime class [ $dateObj->add(new DateInterval('P1M')); $dateObj->format('Y-m-d H:i:s'); ]: |
21 | - | $last_day_next_month = date('t', strtotime('next month')); |
21 | + | '2012-07-01 14:59:19' |
22 | ||
23 | - | $dateObj = new DateTime(); |
23 | + | Get last day of the current month/Get number of days in the current month [ date('t', strtotime('today')); ]: |
24 | - | // Period 1 month --> http://www.php.net/manual/en/dateinterval.construct.php |
24 | + | '31' |
25 | - | $dateInterval = 'P1M'; |
25 | + | |
26 | - | $dateObj->add(new DateInterval($dateInterval)); |
26 | + | Get last day of the next month/Get number of days in the next month [ date('t', strtotime('next month')); ]: |
27 | - | $dateTime_next_month_timestamp = $dateObj->format('Y-m-d H:i:s'); |
27 | + | '31' |