strtotime not working with mm-dd-yyyy

ⅰ亾dé卋堺 提交于 2019-12-10 11:54:46

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/8554414/strtotime-not-working-with-mm-dd-yyyy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!