簡體   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