简体   繁体   English

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

[英]How to strtotime date format like this

I would like to strtotime dateformat like this - Fri Jun 15 05:38:11 +1000 2012 How could I do that? 我想这样的strtotime dateformat-Fri Jun 15 05:38:11 +1000 2012我该怎么做? Basically I need to strtotime it, then do the following - $ago = time()-strtotime(Fri Jun 15 05:38:11 +1000 2012); 基本上我需要strtotime,然后执行以下操作- $ago = time()-strtotime(Fri Jun 15 05:38:11 +1000 2012); and then do this - 然后执行此操作-

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

So I need one thing - strtotime(Fri Jun 15 05:38:11 +1000 2012) which will work as it need to work. 所以我需要一件事-strtotime(Fri Jun 15 05:38:11 +1000 2012)可以正常工作。

If this type of date cannot be strtotime, then how can I edit it, so it could be strtotime? 如果这种类型的日期不能是strtotime,那么我该如何编辑它,所以它可能是strtotime?

Try this (OOP style, there's a procedural equivalent): 尝试以下操作(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()方法允许您定义要解析的日期字符串的格式。

Compare the difference and use floor/ceil to round. 比较差异并使用地板/天花板四舍五入。

$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