问题
I've launch a project recently (here), and I'm stuck with a timer/time question :
I have a cronjob who runs everyday, twice a day : midnight and midday in french timezone.
I want to display a "countdown till next page", which is in fact a countdown till cronjob.
I'm a bit stuck, because when I start to think time and timezone related things, my brain just freeze.
I know I should take server side time, and display my variables in a client side timer (without getting the client side time), but that's pretty much it.
I'd really appreciate some advises !
Thanks in advance to all of you !
回答1:
You can use php DateTime for this.
- The current time
- The midday time
- The midnight time
Here is an example.
$now = new DateTime;
$morning = new DateTime('08:00');
$night = new DateTime('22:00');
// next time is night
if( $now > $morning && $now < $night )
{
$time = $now->diff($night);
}
// next time is day
else
{
$time = $now->diff($morning);
}
$target = $time->format('%h:%i:%s'); // returns a string.
回答2:
You should use Keith Wood's countdown timer: http://keith-wood.name/countdown.html
It is extremely easy to use.
All you have to do is
$('#timer').countdown({
until: '<?php echo date("h:i:s"); ?>' // change this, obviously
});
Here's the fiddle: http://jsfiddle.net/tqyj4/289/
来源:https://stackoverflow.com/questions/17733345/timer-timezone-server-side-time-client-side-countdown