繁体   English   中英

如何使用strtotime这样的日期格式

[英]How to strtotime date format like this

我想这样的strtotime dateformat-Fri Jun 15 05:38:11 +1000 2012我该怎么做? 基本上我需要strtotime,然后执行以下操作- $ago = time()-strtotime(Fri Jun 15 05:38:11 +1000 2012); 然后执行此操作-

$ago = $ago / (60*60*24);
echo $ago.' days ago';

所以我需要一件事-strtotime(Fri Jun 15 05:38:11 +1000 2012)可以正常工作。

如果这种类型的日期不能是strtotime,那么我该如何编辑它,所以它可能是strtotime?

尝试以下操作(OOP样式,有等效的过程):

<?php
$str = "Fri Jun 15 05:38:11 +1000 2012";

// From here: http://is2.php.net/manual/en/datetime.createfromformat.php
$datetime = DateTime::createFromFormat('D M d H:i:s T Y', $str);
$now = new DateTime();

// From here: http://is2.php.net/manual/en/datetime.diff.php
$interval = $now->diff($datetime);
echo $interval->format("%R%a days");
?>

DateTime::createFromFormat()方法允许您定义要解析的日期字符串的格式。

比较差异并使用地板/天花板四舍五入。

$now = time();
$then = strtotime($var);

echo floor(($now - $then) / 86400) . " days ago";

暂无
暂无

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

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