当前位置: 动力学知识库 > 问答 > 编程问答 >

timestamp - PHp calculate Time Difference (ignore dates) from unix time

问题描述:

What's the simplest way to do this?

$known_time = '19:33:39' //GMT

I want to get the current GMT Time and calculate if it is past $known_time

I do not care about the dates, I simply want to know if the time of day right now in GMT is later than $known_time

My attempts so far were clumsy as I first checked the hour...than the minute. I'm sure there's a more elegant method. Thank you!

网友答案:

I don't know what elegant is for you but alternatively, you could just also DateTime objects:

$now = new DateTime('GMT');
$known_time = new DateTime('19:33:39', new DateTimeZone('GMT'));
if($now >= $known_time) {
    echo 'now is greater';
} else {
    echo 'now is less than the time you provided';
}
网友答案:

you can use PHP 5.2 or above and the following functions are available there:

$date_a = new DateTime('2010-10-20 08:10:00');
$date_b = new DateTime('2008-12-13 10:42:00');

$interval = date_diff($date_a,$date_b);

echo $interval->format('%h:%i:%s');

or what you can do is use: strtotime($yourtime)-strtotime(othertime), this will give you the difference.and then use round() to basically round the numbers..

分享给朋友:
您可能感兴趣的文章:
随机阅读: