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

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

问题描述:

I'm struggling with a RegEx to change a date/time string to a timestamp. The format of the string is 'd/m/Y \a\t g:ia' (e.g. 15/06/2012 at 6:00pm).

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. Thanks.

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

Thanks.

我正在努力使用RegEx将日期/时间字符串更改为时间戳。 字符串的格式是'd / m / Y \ a \ tg:ia' code>(例如,2012年6月15日下午6:00)。 p>

我需要这样做以确保日期不在过去。 p>

我可以做日期部分(只需'd / m / Y' code>) 使用下面的代码,但这是我正在努力的其余部分。 谢谢。 p>

  $ expires = preg_replace(“/(\ d {2})\ /(\ d {2})\ /(\ d {4})/”  ,“\\ 3  -  \\ 2  -  \\ 1”,$ poll ['poll_expired']); 
  code>  pre> 
 
 

谢谢。 p>

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.)