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
Calculating remaining time in days,hours and mins
Moderator: General Moderators
-
psmshankar
- Forum Commoner
- Posts: 96
- Joined: Tue Aug 06, 2002 4:25 am
- Location: India
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 '';
}
?>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)
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:
-
psmshankar
- Forum Commoner
- Posts: 96
- Joined: Tue Aug 06, 2002 4:25 am
- Location: India
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