Show time left with PHP
Let say you have a time or date in the future, and want to show your visitor the time left until that time.
I want to show something like:
- 1 Day(s), 5 Hours
- 4 Hour(s), 35 Minute(s)
- 5 Second(s)
The function must be able to output any of these examples. The first example probably have some minutes and seconds aswell left, but they are not interesting so we only show days and hours. The same way if we are down ad hours, then we only show hours and minutes and so on…
| function timeLeft($theTime) { $now = strtotime(“now”); $timeLeft = $theTime – $now; if($timeLeft > 0) if($days) return $theText; |
The first part is “only” some math to seperate the days, hours, minutes and seconds (left). If it’s one day and two seconds, we get back $days = 1, $hours = 0, $minutes = 0, $seconds = 2. The techniques used is called div and mod (/ and % operators). The give you back either how many whole times you can dived or the rest after a divition. Anyway, they are great for getting this information
don’t bother if you don’t care…
What I do after that is to check if we have any days and in that case write the number of days, check if we have minutes and if that is the case write them aswell.
If we didn’t have any days, we do a check to see if we have any hours… and so on and so on. the last way we can take is when we only have seconds left. Then we show only the seconds.
(Of course we begin with checking that the date is in the future, otherwise we return “the time is already in the past!“)
And of course I will give you a small example for testing the function:
| $testTime = strtotime(“next Thursday”); echo timeLeft($testTime); $testTime = strtotime(“+1 week 2 days 4 hours 2 seconds”) |
Obs, you need to feed the function with a unix time stamp, which strtotime is great for…
Here you get two dates/times to try it with.
Similar Posts:
- Show time spent on a site when they leave
- Date of Yesterday
- Generate text with transparent background
- Schedule scripts to run
- Random image with javascript

June 11th, 2009 at 1:10 am
sorry to say but, what a shitty code !!
June 17th, 2009 at 7:55 am
It’s nothing beautiful I know. It’s more a way to give some people an introduction on how to work with this kind of functions.
July 12th, 2009 at 2:50 am
thanks it helps me a little.
August 4th, 2009 at 4:43 pm
I used a bit of this code for a CakePHP project:
class countdownComponent extends Object {
function timeleft($until = ‘Dec 25 2012 00:00:00′) {
$now = strtotime(‘now’);
$deadline = strtotime($until);
$difference = $deadline – $now;
if ($difference > 0) {
$timeleft = array(
‘days’ => floor($difference/60/60/24),
‘hours’ => $difference/60/60%24,
‘minutes’ => $difference/60%60,
’seconds’ => $difference%60,
);
} else {
$timeleft = array(
‘days’ => 0,
‘hours’ => 0,
‘minutes’ => 0,
’seconds’ => 0
);
}
return $timeleft;
}
}
August 11th, 2009 at 7:24 am
A little bit cleaner than the example we provided. Thanks!
August 19th, 2009 at 5:17 am
sorry for my previous comment, I am not a coder and realy don’t know what makes code good – that is why I was angry.
August 27th, 2009 at 8:40 am
thanks its simple but good script