PHP gmdate到时间戳


PHP gmdate to timestamp

我有一个日期字符串,格式如下1970年1月1日,我很难将其转换回时间戳格式

我尝试过使用Datetime,但无效

$test=new DATETIME($expire_date);
echo date_timestamp_get( $test);

我知道这是用代码制作的

<?php
gmdate("F j, Y", $date_created)

如何将其格式化为时间戳,以便与今天的日期进行比较例如

<?php
if (time()>$timestamp){
//do something
}

您需要将DateTime::format()U参数一起使用,因为$test是DateTime对象,而不是常规日期字符串:

$timestamp = $test->format('U');

或者,更好的是,只比较DateTime对象(我更改了变量名,使代码更容易阅读):

$expires = new DateTime($expire_date);
$now     = new DateTime();
if ($now > $expires){
//do something
}