输出过去日期和今天php之间的时间

时间:2016-08-30 07:40:19

标签: php date strtotime

目前,我有一个查询,从外部网站检查日期,并在该日期超过四周时向我提供一些信息:

$json = json_decode($result, true);

echo date("d.m.Y",strtotime($json[lastUpdated][when]));

$mydate = strtotime($json[lastUpdated][when]);

if ($mydate <= strtotime('4 weeks ago')) {
    echo "Is not up to date!";
}

我想知道日期$mydate和今天之间已经过了多少时间。谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用以下方法计算两个日期之间的差异:

$mydate = strtotime($json['lastUpdated']['when']); // I guess the quotes are missing in your code
$now = time();
echo "The difference is " . ($now - $mydate) . " seconds";

如果你需要不同的东西,那么几秒钟你就不得不计算这些值,例如

echo "The difference is " . (($now - $mydate)/60) . " minutes";
echo "The difference is " . (($now - $mydate)/60*60) . " hours";
echo "The difference is " . (($now - $mydate)/60*60*24) . " days";
相关问题