简体   繁体   English

如何在PHP中提取“日期字符串”的“格式字符串”

[英]How to extract 'format string' of a 'date string' in PHP

Date-Time is read from external resource and stored in $dt, And it could be in any valid PHP Date-Time format (valid means date-time is already formated by a string containing letters described here on php.net manual ). 从外部资源读取Date-Time并将其存储在$ dt中,并且可以采用任何有效的 PHP Date-Time格式(有效表示date-time已由php.net手册上所述的包含字母的字符串格式化)。

What I need to do is to get format of the date-time string, So after manipulating the date to system needs, I can reformat it to the same string and store it in database. 我需要做的是获取日期时间字符串的格式,因此在根据系统需求调整日期之后,我可以将其重新格式化为相同的字符串并将其存储在数据库中。 Simply put: do opposite of DateTime::format method 简而言之:与DateTime :: format方法相反

Is there any particular method which extracts 'format string' of a formated date-time? 是否有任何特定方法可以提取格式化日期时间的“格式字符串”? or an available code? 或可用的代码?

It is not possible, as the formatted dates can not contain enough information. 这是不可能的,因为格式化日期不能包含足够的信息。 You will have to write some set of regexp to match your formats which are really used as your inputs - one by one. 您将必须编写一组正则表达式来匹配您真正用作输入的格式-一对一。

You could take them all to a unix timestamp and then use date to get a standardised form. 您可以将它们全部带入unix时间戳,然后使用date获得标准化的表格。 Or you could standardise your input in the first place (which is usually the easier option - because error catching is going to be a mare!) 或者,您可以首先标准化您的输入(通常这是更简单的选择-因为错误捕获将成为母马!)

\\$dt your datetime input
$timestring = strtotime($dt);
$datetime = date("H:i:s j M Y", $timestring);

Of course, you might get some funny outputs from strtotime if your inputs are weird. 当然,如果您的输入很奇怪,您可能会从strtotime获得一些有趣的输出。

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

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