How can we find out how many time remains to end the current day from current time[date('Y-m-d H:i:s')] in PHP.
+4
A:
For example with a combination of mktime() and time():
$left = mktime(23,59,59) - time() +1; // +1 adds the one second left to 00:00
Update:
From Simon's suggestion, mktime(24,0,0) works too:
$left = mktime(24,0,0) - time();
Felix Kling
2010-05-29 09:52:22
You should add 1 second, then your result is perfect.
Simon
2010-05-29 09:55:56
@Simon: Noticed that and already edited it ;) Thank you.
Felix Kling
2010-05-29 09:57:33
Would mktime(24,0,0) also work? I don't have the possibility to test it now.
Simon
2010-05-29 10:01:50
@Simon: I just tried it (for fast testing I use codepad.org) and it works indeed :)
Felix Kling
2010-05-29 10:03:19
Cool site! Thanks!
Simon
2010-05-29 10:06:39
May not work if dailight saving time kicks in that day. You sould use the DateTime class.
Artefacto
2010-05-29 16:22:53
A:
$time_in_seconds = (24*3600) - (date('H')*3600 + date('i')*60 + date('s'));
Calculates the total seconds of one day and subtracts the seconds passed until the current hour of the day.
Simon
2010-05-29 09:52:39
A:
In Javascript, for anyone interested (takes care of timezone differences) :
var now=new Date();
var d=new Date(now.getYear(),now.getMonth(),now.getDate(),23-now.getHours(),59-now.getMinutes(),59-now.getSeconds()+1,0);
document.write(checkTime(d.getHours()) + ':' + checkTime(d.getMinutes()) + ':' + checkTime(d.getSeconds()) + ' time left');
function checkTime(i) {
if (i<10)
{
i="0" + i;
}
return i;
}
stagas
2010-05-29 10:20:14