Php – strtotime not working with mm-dd-yyyy

dateformattingPHPstrtotime

I have a script which is fed dates in numerous different formats.

I want to save these dates as timestamps so they can easily be manipulated/ordered.

When i try an convert a mm-dd-yyyy type date to a timestamp, it fails.

When the script runs, it does not know what format it will be fed, and as such this cannot be specified. Near all other formats of date seem to be converted fine.

Could anyone advise how to fix this, or alternatively an alternative way that all date formats can be converted to an orderable, consistent format that can be manipulated?

Many Thanks

Best Answer

It sees strings with - in them as dd-mm-yyyy and / as mm/dd/yyyy.

See also this question and the comments on the documentation.

Possible solutions / workarounds:

  • on php 5.3, use date_create_from_format
  • on older php and not on windows, use strptime
  • if neither can be used, either replace the - to / when necessary, or use one of the regexes suggested you can find through the linked question.

Note however that at some time you do need to know what the format is to start with. Computers are not mindreaders. They can't, and never will be able to, distinguish between mm-dd-yyyy and dd-mm-yyyy in the overlap ranges (both mm and dd <= 12) if you don't provide the distinction.