开发者

PHP DateTime Regex

开发者 https://www.devze.com 2022-12-30 09:30 出处:网络
Hey, long story short I have inherited some terrible code. As a result a string comparison is buggy when comparing dates due to the format of the date. I am trying to convert the date to a valid DateF

Hey, long story short I have inherited some terrible code. As a result a string comparison is buggy when comparing dates due to the format of the date. I am trying to convert the date to a valid DateFormat syntax so I can run a proper comparison.

These are some samples of the current format:

12/01/10 at 8:00PM

12/31/10 at 12:00PM

12/10/09 at 5:00AM

and so forth. I'd like to convert this to a YYYYMMDDHHMM format i.e 201012012000 for comparison purposes. If anyone can give me a quick regex snippet to do this that'd be appreciated as right now i'm hitting a brick wall for a regex. 开发者_如何学PythonI can do it by exploding the string over several times etc but I'd rather do it in a more efficient manner.

Thanks!


Working with dates in strange formats is very easy with the DateTime class which was built into PHP 5.3.

No need for regex or anything fancy:

$date = DateTime::createFromFormat('m/d/y \a\t g:iA', '12/10/09 at 5:00AM');
print_r($date);

Once it is a date object you can have it in any format you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消