PDA

View Full Version : How to calculate days elapsed?


quadrant6
02-24-2005, 09:10 PM
I need to show days elapsed for each record and all I have is a date. I figured there must be a way to get the difference in days (date <> today's date) but can find no function for this.

Anyone know of a simple way?

delinear
02-24-2005, 09:44 PM
Well, you could use mktime() to turn your two dates into unix timestamps then deduct one from the other and divide the result by 86,400 (the number of seconds in a day) to find the number of days difference.

marek_mar
02-24-2005, 09:50 PM
If you have the time in UNIX timestamps you can just substract from the current date. If you don't have the date in a timestamp you can use strtotime() (http://www.php.net/strtotime) or mktime() (http://www.php.net/mktime) (whatever fits) and format it with the date() (http://www.php.net/date) function.
You should also read this thread (http://www.codingforums.com/showthread.php?t=50759).

devinemke
02-24-2005, 10:08 PM
if you are using mySQL then use the DATEDIFF (http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html) function (if possible).

quadrant6
02-24-2005, 11:28 PM
Well here is my attempt and it's not quite working:


$today = date('Ymd');


function days_elapsed($date_entered)
{
global $today;
$m = substr($date_entered, -4, 2);
$d = substr($date_entered, -2);
$y = substr($date_entered, -8, 4);
$date_entered = date("Ymd", mktime(0, 0, 0, $m, $d, $y));
$elap=$date_entered-$today;
$elapsed = getdate($elap);
return $elapsed[days];
}


echo "days since Jan 21: ".days_elapsed('20050121');




Basically, '20050121' is the format I will be getting from the database (string - year,month,day)

marek_mar
02-25-2005, 01:53 AM
Ok... It's 3AM... that's why it's so quick.

<?
function days_elapsed($time /* feel free to use mktime() to generate a timestamp*/)
{
if (!(is_int($time) && strlen($time) == 11))
{ // Let's say this is an unneeded feature.
$time = strtotime($time);
}
$diff_date = time() - $time;
$more_than_year = date('Y', $diff_date) - 1970; // Hack for non negative timestamps.
$years = ($more_than_year >= 0) ? $more_than_year : '';
return $years * 365 + date('z', $diff_date);
}


echo 'days since Jan 21 2005: '.days_elapsed('21 Jan 2005') . "\n";
?>

Store UNIX timestamps in your DB not the mysql timestamp.
It's not perfect due to leap years.

Ultragames
02-25-2005, 03:33 AM
$test1 = mktime(0,0,0,8,14,5);
$test2 = mktime(0,0,0,2,23,5);
$test3 = ($test1 - $test2) / 86400;
print"$test3";

mktime goes hour, min, sec, month, day, year.

the first mktime is your oldest date, the second one your newer date. It will return the differance in days. Change the divider, (86400) to get other ammounts.

quadrant6
02-27-2005, 07:53 PM
thanks, the following seems to work:


$today = mktime(0,0,0,date('m'),date('d'),date('Y'));

function days_elapsed($date_entered)
{
global $today;
$m = substr($date_entered, -4, 2);
$d = substr($date_entered, -2);
$y = substr($date_entered, -8, 4);

$date_entered_time = mktime(0,0,0,$m,$d,$y);
echo $today. " " . $date_entered_time;
$elapsed = ($today - $date_entered_time) / 86400;
return $elapsed;
}


echo "days since Jan 21: ".days_elapsed('20050121');