Reputation: 5
I have two dates:
$today = '2012-12-01 10:40:00';
$check = '2012-12-03 12:00:00';
How can I show countdown for this dates? Should show me:
Count: 49 hours and 20 minutes. I can check only hours or only minutes with function mktime, but how can i compare this?
Upvotes: 0
Views: 2731
Reputation: 2125
Assuming you want the clock to keep ticking as the user stays on the page, you don't really want to do that using PHP (unless you want to dispatch AJAX calls to the server every second to update the clock, which would suck). Do it client-side, using javascript.
Here are 25 pretty scripts that do that using jQuery: http://www.tripwiremagazine.com/2012/11/jquery-countdown-scripts.html
Upvotes: 1
Reputation: 219794
try using DateTime::diff
<?php
$datetime1 = new DateTime('2012-12-01 10:40:00');
$datetime2 = new DateTime('2012-12-03 12:00:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%1 day %h hours %i minutes');
?>
Upvotes: 4
Reputation: 99505
You can change these dates to a unix timestamp with strtotime
.
Then you can calculate the difference between them in seconds.
60 seconds in a minute, 60 minutes in an hour.
Upvotes: 2