15,304,276 members
See more:
how we can implement the countdown timer in php
exemple:Auction end in: 5 days 5 hours 4 minutes 4 sec

Posted
Updated 29-Aug-13 7:05am
v2
ZurdoDev 29-Aug-13 13:17pm

Use javascript. There are likely jquery timer plugins you could use.

## Solution 1

You can do it just by manipulating 2 integers, followed by dividing them out to give you the num of days, hours, mins, secs.

You'd just want to base both times - current and expiration, on the unix epoch.

Use the php function time() to get the time since the epoch. Pass this number to the javascript. Using javascript, get the elapsed time since the epoch, at the current time. Subtract 1 from the other, do some dividing and et voila! You've the time remaining.

Now, run the func to calculate the time remaining once every second, minute, whatever.
Without the timer part, this is how the forum is able to tell you that something was posted seconds, minutes, hours ago or tell you it was 'yesterday' etc, etc.

http://stackoverflow.com/questions/1090869/why-is-1-1-1970-the-epoch-time

Here's an exerpt from an old project I have.
PHP
```function makeNiceTime(\$intTime)
{
\$curTime = time();
\$strTime = '';
if ( (\$curTime-\$intTime) <  (60)) //(24*60*60))
{
\$strTime = sprintf("%d seconds ago", \$curTime-\$intTime);
}
else if ( (\$curTime-\$intTime) <  (60*60)) //(24*60*60))
{
\$strTime = sprintf("%d minutes ago", (\$curTime-\$intTime) / 60 );
}
else if ( (\$curTime-\$intTime) <  (60*60*24)) //(24*60*60))
{
\$strTime = sprintf("%d hours ago", (\$curTime-\$intTime) / (60*60) );
}
else if ( (\$curTime-\$intTime) <  (60*60*24*7)) //(24*60*60))
{
\$strTime = sprintf("%d days ago", (\$curTime-\$intTime) / (60*60*24) );
}
else
{  // sample: "12.22 am Sat 21-Jul 2012"
\$strTime = date("g.i a D j-M Y", \$intTime);
}
return \$strTime;
}```