Calculating remaining time in days,hours and mins

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Calculating remaining time in days,hours and mins

Post by psmshankar »

Hi,
I want to calculate the remaining time in days then hours then mins....

my situation is like this..
i have a table that contains the bid details of an item. i have the starting date and closing date for bidding...
in a web page, i want to display all the details for that item in a table...

in the last column i want to display
1. how many days remaining to bid for that item (5 days or 6 days ...etc).
2. If it is less than a day, then i need to display the remaining hours ( 12hrs or 9hrs..etc..)
3. if it is less than an hour, in that case i need to display the remaining time in minutes... (40mins or 25 mins etc..)
4. if it is less than a minute, i need to display the remaining time in seconds... (4 secs or 15 secs etc..)

Thanks.
Shankar
redJag
Forum Newbie
Posts: 18
Joined: Fri Jan 31, 2003 12:17 am

Post by redJag »

no offense, but how do you setup a system as complex as bid/auction and not know how to subtract close time - current time? Just go to php.net/manual and search for getdate.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

from the world of irc and mIRC scripts back in the day I was used to a duration function, so when I started using PHP I found a piece of a perl script somewhere and changed it a bit made a php version, basically it converts seconds to minutes,hours,days, so you could take the unixtime of the end-date/time and subtract current unixtime and feed to this function..

Code: Select all

<?php

function duration ($sec) {
    return
     trim (
      m(32659200,$sec,'year')
      .m(2721600,$sec,'month')
      .m(604800,$sec,'week')
      .m(86400,$sec,'day')
      .m(3600,$sec,'hour')
      .m(60,$sec,'minute')
      .m(1,$sec,'second')
     );
}

function m($cycle,&$seconds,$unit) {
  if ($seconds >= $cycle) {
    $r=($a=((int)($seconds/($cycle)))).' '.$unit;
    if ($a!=1) $r.='s';
    $seconds -= $a*$cycle;
    return $r.' ';
  } else return '';
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you might also use javascript to create a real countdown.
google found e.g. this: http://www.tdscripts.com/jscountdown.html (example date is in the past, so do not wonder ;) )
If you choose js make sure to provide enough information even if js is disabled (or unsupported)
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

i find the easiest way to manipulate time is with a UNIX timestamp cause it's in seconds.
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

i got it...and finally came up with a function to calculate... :::

Code: Select all

<?php
//#######################################################
//  Function to calculate remaining days, hours, minutes and seconds between current date and end date..
//  Returns the remaining time .. in days or hrs or mins or secs...
//#######################################################
function CalRemTime($ClosingDay,$ClosingMonth,$ClosingYear)
{
	$ClosingTime = mktime(24,60,60,$ClosingMonth,$ClosingDay,$ClosingYear);
	$TimeDifference = $ClosingTime - time();

	$RemainingDays = ($TimeDifference - ($TimeDifference % 86400)) / 86400;
	$TimeDifference = $TimeDifference - ($RemainingDays * 86400);
	$RemainingHours = ($TimeDifference - ($TimeDifference % 3600)) / 3600;
	$TimeDifference = $TimeDifference - ($RemainingHours * 3600);
	$RemainingMinutes = ($TimeDifference - ($TimeDifference % 60)) / 60;
	$TimeDifference = $TimeDifference - ($RemainingMinutes * 60);
	$RemainingSeconds = ($TimeDifference - ($TimeDifference % 1)) / 1;

	if($RemainingDays>0)
	{
		$sValue = "$RemainingDays days";
		return $sValue;
	}
	else	
	{
		if($RemainingHours>0)
		{
			$sValue = "$RemainingHours hrs:$RemainingMinutes mins:$RemainingSeconds Secs";
			return $sValue;
		}
		else
		{
			if($RemainingMinutes>0)
			{
				$sValue = "$RemainingMinutes mins:$RemainingSeconds Secs";
				return $sValue;
			}
			else
			{
				if($RemainingSeconds>0)
				{
					$sValue = "$RemainingSeconds secs";
					return $sValue;
				}
				else
				{
					$sValue = "Bidding time is over...";
					return $sValue;
				}
			}
		}
	}
}

?>
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

redJag:

i think there is no offense to get ideas and suggestions to put forth first...
always u can learn from others a lot....
and there is nothing wrong in getting ideas and suggestions...
but the final decision shud be yours....
anyway thanks a lot for ur suggestion...
Post Reply