How to convert assumed American or European date format so that PHP strtotime() works.

The strtotime() PHP function assumes the American format of a date when the delimiters are slashes (i.e., "m/d/y"). For example, "31/01/2012" is assumed to be an American date, and thus the strtotime() function won't work here.

If you use periods in the date (for example, "31.01.2012"), the European format is assumed.

See, where they explain this:

Convert European date assumed to be American date to European date so strtotime works

So, if you want to convert a European date that is using slashes to a date format the strtotime() function understands, the easiest way is just to replace the slashes with periods. This way, the European format of "d/m/y" is assumed.

str_replace("/", ".", "31/01/2012");

The above function would return "31.01.2012".

Convert American date assumed to be European date to American date so strtotime works

Alternatively, if you need to convert an American date that is in the European format, you can convert the periods (or dashes) with slashes. For example:

//If uses dots
str_replace(".", "/", "12.01.2012");

//If uses dashes
str_replace("-","/", "12-01-2012");

This would return "12/01/2012".

I hope this helps.

Want to learn how to code and make money online? Check out CodingPhase (referral link)