我正在尝试将日期-时间作为字符串的变量转换为时间戳对于下面的代码,它总是将输出作为01/01/1970 00:01:00
无论字符串中的日期时间是多少
mydate是一个日期时间选择器
在mydate中,日期是通过设置的
functions.php
$randnos=rand(0,30);
$randhrs=rand(00,24);
$randmin=rand(00,60);
$randsec=rand(00,60);
$ttxt='+'.$randnos.' days +'.$randhrs.' Hours +'.$randmin.' minutes +'.$randsec.' seconds';
$newTime = date("d/m/Y H:m:s",strtotime($ttxt));
update_post_meta($post_id,'mydate',$newTime);
模板
$tdate=new DateTime();
$mystring=get_field('mydate');
$timestamp = DateTime::createFromFormat('d/m/Y H:m:s', $mystring)->getTimestamp();
$mystring= date("d/m/Y H:m:s", $timestamp );
if ($mystring>=$tdate){
echo "yahoo";
}
并且它从不传递if条件加上它给了我对非对象上的成员函数getTimestamp()的调用
问题似乎是strtotime无法读取时间字符串并将其转换为时间戳。
使用DateTime对象转换字符串怎么样
$mystring="25/12/2015 14:12:54";
$timestamp = DateTime::createFromFormat('d/m/Y H:m:s', $mystring)->getTimestamp();
echo $timestamp . '=' . date("d/m/Y H:m:s", $timestamp );