I have a date like 3/3/2012 10:56:34
and i'd like to convert it to a countdown (like ebay).
It doesn't have to be dynamic, just something like
3h 2m
2d 4h
3m
etc
It doesn't have to show years, just days, hours, mins, secs, whichever is applicable.
So if there's over a day to go, it'll show days and hours, if under a day, just hours mins, if under an hour, just mins.
Is there a simple way to do this?
This is what I have, but doesn't work (fixed)
$timediff = round(strtotime($rs[ends]) - strtotime($now));
while ($timediff > 86400) { $timediff = $timediff - 86400; $days++; }
while ($timediff > 3600) { $timediff = $timediff - 3600; $hours++; }
while ($timediff > 60) { $timediff = $timediff - 60; $mins++; }
$secs = $timediff;
echo $days . "d " . $hours . "h " .$mins . "m";
Use strtotime to convert your string to the Unix timestamp, then calculate the difference between now and the timestamp and then you'll easily manage to extract days, hours, minutes, etc...
Example for secs and mins from the top of my head (applicable for $timediff < 1 hour, in your case):
$seconds = $timediff % 60; $minutes = floor($timediff / 60);
etc, etc...
first use strtotime to convert to the unix time stamp if necessary.
simply calculate the times you want it to count down by:
1day is 86400 seconds.
1hour is 3600 seconds
Use SQL to get the time stamp then simply if and ifelse statements.
$currenttime=time();
if($currenttime-$somearray[0][time_stamp]<3600)
{
$seconds=$curenttime-$somearray;
echo"$seconds seconds ago";
}
elseif($currenttime-$somearray[0][time_stamp}<86400)
{
$seconds=$currenttime-$somearray;
$minutes=floor($seconds/60);
echo"$minutes minutes ago";
}
and just continue the elseif statements for whatever intervals you would like.