Skip to content

Instantly share code, notes, and snippets.

@Om4ar
Forked from garak/strtotime.php
Created March 26, 2018 20:38
Show Gist options
  • Save Om4ar/cb795844a32bb9c8825651f81f7fb70c to your computer and use it in GitHub Desktop.
Save Om4ar/cb795844a32bb9c8825651f81f7fb70c to your computer and use it in GitHub Desktop.
never use '+1 month' and '-1 month' in strtotime. It's just bugged
<?php
// tried this today, 31 May 2011
echo "\n";
echo date('Y-m-d', strtotime('first day of next month')); // correct
echo "\n";
echo date('Y-m-d', strtotime('+1 month')); // wrong! output is 2011-07-01
echo "\n";
echo date('Y-m-d', strtotime('next month')); // as above, output is 2011-07-01
echo "\n";
echo date('Y-m-d', strtotime('first day of previous month')); // correct
echo "\n";
echo date('Y-m-d', strtotime('-1 month')); // wrong! output is 2011-05-01
echo "\n";
echo date('Y-m-d', strtotime('2011-05-29 first day of previous month')); // correct (nice!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment