我有一个这样的字符串日期:2015/12/20 13:58:59
我尝试转换时间戳:
$idate = $user->multiexplode(array("/"," ",":"),strip_tags("2015/12/20 13:58:59"));
//mktime( $hour , $minute , $second , $month , $day , $year , $is_dst );
$timestamp = mktime($idate[3],$idate[4],$idate[5],$idate[1],$idate[2],$idate[0]);
现在我尝试转换真实日期:
echo 'new date: '.jdate('Y/n/j H:i:s',$timestamp);
好的......它有效,但有问题!
根据时间服务器,我得到可变时间。
for location for location +1 GMT:2015/12/20 14:58:59
for -1 GMT:2015/12/20 11:58:59
我希望所有服务器再次打印2015/12/20 13:58:59
答案 0 :(得分:0)
您可以使用DateTime
类及其方法将日期和时间转换为unix时间戳。并使用strftime()
将unix时间戳转换回所需的格式,如下所示:
$datetime = "2015/12/20 13:58:59";
// covert timestamp
$unixdatetime = DateTime::createFromFormat('Y/m/d H:i:s', $datetime)->getTimestamp();
echo $unixdatetime . "<br />"; // 1450616339
// now format the unix timestamp
$formatted_datetime = strftime("%Y/%m/%d, %H:%M:%S",$unixdatetime);
echo $formatted_datetime; // 2015/12/20, 13:58:59
输出:
1450616339
2015/12/20, 13:58:59
以下是参考资料:
答案 1 :(得分:0)
使用此代码。
<?php
$date = '2015/12/20 13:58:59';
//convert to timestamp
$timestamp = strtotime($date);
echo $timestamp;
//convert timestamp back to datetime
$date_again = date('Y/m/d H:i:s', $timestamp);
echo $date_again;
?>