tags:

views:

58

answers:

3

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
You should add 1 second, then your result is perfect.
Simon
@Simon: Noticed that and already edited it ;) Thank you.
Felix Kling
Would mktime(24,0,0) also work? I don't have the possibility to test it now.
Simon
@Simon: I just tried it (for fast testing I use codepad.org) and it works indeed :)
Felix Kling
Cool site! Thanks!
Simon
May not work if dailight saving time kicks in that day. You sould use the DateTime class.
Artefacto
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
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