1

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

Thomas Clowes
  • 4,529
  • 8
  • 41
  • 73
  • There isn't any code to show.. it is a theoretical question. Why doesn't mm-dd-yyyy formats work in strtotime, and is there a way of converting dates in any format to a single manipulatable format..? – Thomas Clowes Dec 18 '11 at 20:21
  • I think the issue is that there is a `dd-mm-yyyy` format as well, and the two are impossible to tell apart. I think here is a duplicate: – Pekka Dec 18 '11 at 20:22
  • possible duplicate of [strtotime failing on mm-dd-yyyy hh:mm](http://stackoverflow.com/questions/5133703/strtotime-failing-on-mm-dd-yyyy-hhmm) – Pekka Dec 18 '11 at 20:22
  • strtotime() uses American-style dates, which means that if it finds a date like 10/11/2003, it will consider it to be October the 11th as opposed to November the 10th. – Julien Dec 18 '11 at 20:24
  • Thanks for the links. On this basis is there any uniform way of converting both dates in the mm-dd-yyyy format and dd-mm-yyyy format into a single manipulatable format? – Thomas Clowes Dec 18 '11 at 20:44
  • A date such as 03-24-2009 does not get converted.. because it is not a valid strtotime format as mentioned above.. – Thomas Clowes Dec 18 '11 at 21:25

1 Answers1

10

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.

Community
  • 1
  • 1
Inca
  • 1,891
  • 14
  • 15
  • Thanks for the links. On this basis is there any uniform way of converting both dates in the mm-dd-yyyy format and dd-mm-yyyy format into a single manipulatable format? – Thomas Clowes Dec 18 '11 at 20:44