PHP: Strtotime and Handling Different Date Formats Programming 27 OCT 2010

green php elephants - elephpantsStrtotime is a powerful date manipulating ally of any PHP developer, but unfortunately it doesn’t really respond to locale settings all that well, meaning that should you not be an American, in other words your dates don’t look like the silly MM/DD/YYYY format, it can return some rather strange and unexpected results!

Funnily enough though, there is in actual fact a pretty simple fix to this – and it all lies with the simple joining character you use to specify your date!

If like me you prefer the more logical European layout of DD-MM-YYYY, then make sure to use a dash (-) as your joining character. The usual forward slash (/) signifies American MM/DD/YYYY format while a humble period (or fullstop if you like) (.) indicates ISO YYYY.MM.DD.

And in action?

echo date("jS F, Y", strtotime("11.12.10")); 
// outputs 10th December, 2011 
echo date("jS F, Y", strtotime("11/12/10")); 
// outputs 12th November, 2010 
echo date("jS F, Y", strtotime("11-12-10")); 
// outputs 11th December, 2010  

Who would have guessed?

Still, damn useful now that you know! :)

Related Posts:

About Craig Lotter

Software developer, husband and dad to two little girls. Writer behind An Exploring South African. I don't have time for myself any more.