简体   繁体   English

PHP时间和日期解析和差异

[英]PHP time&date parse and difference

I have a PHP string of the following form: "[Fri, 23 Nov 2012 20:00:18 +0000]" 我有一个以下形式的PHP字符串: “[星期五,2012年11月23日20:00:18 +0000]”

I'd like to ask how much time passed since then, if it's 1 day and 5 hours etc. How can I do it efficiently? 我想问一下从那时起经过了多长时间,如果是1天5小时等等。我怎样才能有效地做到这一点?

Thanks!! 谢谢!!

有效的方法是将字符串转换为unix时间戳,并根据该逻辑执行逻辑。

You can do 你可以做

<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->d;
?>

DateTime in PHP PHP中的DateTime

Function strtotime returned unixtime for your string Function time return unixtime for current time. 函数strtotime返回字符串的unixtime函数时间返回当前时间的unixtime。 Both function return time in seconds. 两种函数都以秒为单位返回时间 You cat subtracts it and get interval in seconds 你cat减去它并以秒为单位得到间隔

<?php
$logtime = strtotime("Fri, 23 Nov 2012 20:00:18 +0000");
$interval = time() - $logtime;
printf("Interval is: %d hours, %d minutes, %d sec.", 
        $interval/3600, ($interval%3600)/60, $interval%60);
?>
$date = str_replace(array('[',']'),'',"[Fri, 23 Nov 2012 20:00:18 +0000]");

$diff = date_diff(date_create() , date_create($date));

echo $diff->format("Difference: %Y-%M-%d %H:%i:%s");

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

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