PHP Dates

PHP can be fickle at times. Especially with dates. I ran into a situation at work where I needed to figure out the end of the next payperiod and if that that date was on a week day, figure out the Friday before. After finalizing the script, I learned that PHP would prefer the bigger version of the date than the short version. For example:


$date = date('Y-m-d'); //2014-12-11
$new_date = date('n', strtotime($date));

….won’t give you anything back. Why? Once you think of it, it makes sense.

2014-07-11

Could mean November or July depending on the country you’re in. Soooo if you ask PHP for for the full date….

$date = date('j F Y'); //3 July 2014
$new_date = date('n', strtotime($date)); // 4

This will work. Funny huh? Just asking for the bigger version of the date will solve everything. PHP you fickle pickle!

Leave a Reply

Your email address will not be published. Required fields are marked *