简体   繁体   English

strtotime无法与mm-dd-yyyy一起使用

[英]strtotime not working with mm-dd-yyyy

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. 当我尝试将mm-dd-yyyy类型的日期转换为时间戳时,它将失败。

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 非常感谢

It sees strings with - in them as dd-mm-yyyy and / as mm/dd/yyyy. 它看到带有-字符串,其中dd-mm-yyyy和/表示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 在php 5.3上,使用date_create_from_format
  • on older php and not on windows, use strptime 在较旧的php而非Windows上,请使用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. 如果您不提供区分,则它们将无法(也永远无法)在重叠范围(mm和dd <= 12)中区分mm-dd-yyyy和dd-mm-yyyy。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM