Advertisement
Sk8erPeter

PHP: date functions' results related to the NEXT month /1

Jun 18th, 2012
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.10 KB | None | 0 0
  1. <?php
  2.  
  3. // PHP: date functions' results related to the NEXT month are not correct
  4. // http://stackoverflow.com/questions/10834350/php-date-functions-results-related-to-the-next-month-are-not-correct-php-bugs
  5.  
  6.     // testing function for printing out variables
  7.     function my_var_export($stuff, $text = '...'){
  8.         return '<p>'.$text.':</p><pre>'.var_export($stuff, TRUE).'</pre>';
  9.     }
  10.  
  11.     // Current date as a MySQL-compatible timestamp
  12.     $current_date_timestamp = date('Y-m-d H:i:s');
  13.    
  14.     // Get next month as a timestamp
  15.     $next_month_timestamp = date('Y-m-d H:i:s', strtotime('next month'));    
  16.    
  17.     // Get last day of the current month/Get number of days in the current month
  18.     $last_day_current_month = date('t', strtotime('today'));      
  19.  
  20.     // Get last day of the next month/Get number of days in the next month
  21.     $last_day_next_month = date('t', strtotime('next month'));    
  22.  
  23.     $dateObj = new DateTime();
  24.     // Period 1 month --> http://www.php.net/manual/en/dateinterval.construct.php
  25.     $dateInterval = 'P1M';
  26.     $dateObj->add(new DateInterval($dateInterval));
  27.     $dateTime_next_month_timestamp = $dateObj->format('Y-m-d H:i:s');      
  28.    
  29.     echo my_var_export(strtotime('today'), "strtotime('today');");
  30.     echo my_var_export(strtotime('next month'), "strtotime('next month');");
  31.     echo my_var_export($current_date_timestamp, "Current date as a MySQL-compatible timestamp [ date('Y-m-d H:i:s'); ]");
  32.     echo my_var_export($next_month_timestamp, "Get next month as a timestamp [ date('Y-m-d H:i:s', strtotime('next month')); ]");
  33.     echo my_var_export($dateTime_next_month_timestamp, "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'); ]");
  34.     echo my_var_export($last_day_current_month, "Get last day of the current month/Get number of days in the current month [ date('t', strtotime('today')); ]");
  35.     echo my_var_export($last_day_next_month, "Get last day of the next month/Get number of days in the next month [ date('t', strtotime('next month')); ]");
  36.  
  37.     die();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement