简体   繁体   English

使用正则表达式将日期/时间字符串更改为PHP中的时间戳

[英]Change a date/time string to a timestamp in PHP using a Regular Expression

I'm struggling with a RegEx to change a date/time string to a timestamp. 我正在努力使用RegEx将日期/时间字符串更改为时间戳。 The format of the string is 'd/m/Y \\a\\tg:ia' (eg 15/06/2012 at 6:00pm). 字符串的格式为'd/m/Y \\a\\tg:ia' (例如,2012年6月15日下午6:00)。

I need to do this to ensure that a date is not in the past. 我需要这样做以确保日期不是过去的。

I can do the date part (just 'd/m/Y' ) using the following code, but it's the rest that I am struggling with. 我可以使用以下代码执行日期部分(只是'd/m/Y' ),但这是我正在努力的其余部分。 Thanks. 谢谢。

$expires = preg_replace("/(\d{2})\/(\d{2})\/(\d{4})/", "\\3-\\2-\\1" , $poll['poll_expired']);

Thanks. 谢谢。

Why can't you do something like this? 为什么你不能做这样的事情?

$date = DateTime::createFromFormat('d/m/Y \a\t g:ia', ' 15/06/2012 at 6:00pm');
$expires = $date->format('U');

(Not tested but you get the idea.) (没有经过测试,但你明白了。)

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

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