开发者

Time left calculation not working PHP

开发者 https://www.devze.com 2023-02-11 22:14 出处:网络
I am doing a website where I needed to calculate the time left somewhere.both time(), and $endTime work properly individually开发者_JAVA技巧 but this calculation doesn\'t seem to work

I am doing a website where I needed to calculate the time left somewhere.both time(), and $endTime work properly individually开发者_JAVA技巧 but this calculation doesn't seem to work (it always shows 02:00:00):

$timeLeft = date('H:i:s',max(($endTime - time()) , 0));

Where is the problem?

edit: $endTime = 1296727200


The function date expects an timestamp (a number of seconds since 1970/1/1) as argument while $endTime - time() is a duration. You can compute the number of minutes and hours that duration represents as:

$seconds = $endTime - time();
$minutes = $seconds / 60;
$seconds = $seconds - $minutes * 60;
$hours = $minutes / 60;
$minutes = $minutes - $hours * 60;
$timeLeft = sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);

Of course $endTime should not be in the past…


found the answer thanks to the comments, the problem is that the $endTime was already smaller then time(), and then date(0)= January 1970, 02:00:00 in my timezone so it will be 02:00:00

0

精彩评论

暂无评论...
验证码 换一张
取 消