Page 1 of 1

Date differences with future date

Posted: Wed Sep 03, 2003 9:49 pm
by lunartek
I wrote a function that calculates the differences between two dates, however this only work with past dates and I need to be able to use for future dates as well.

Here's the function...

Code: Select all

function DateDiff($date1,$date2,$output){
// Calculates date differences.
// written on sept 3, 2003 by dreamz.com designs.

if (!is_numeric($date1)){ $date1 = strtotime($date1); }
if (!is_numeric($date2)){ $date2 = strtotime($date2); }
$date = $date1-$date2;

$d = floor($date / (3600 * 24));
$h = floor($date/3600 - ($d * 24));
$m = floor($date/60 - ($d * 1440) - ($h * 60));
$s = floor($date - ($d * 86400) - ($h * 3600) - ($m * 60));

$output = preg_replace("/s/","$s secs",$output);
$output = preg_replace("/d/","$d days",$output);
$output = preg_replace("/h/","$h hours",$output);
$output = preg_replace("/m/","$m mins",$output);
return $output;
}
Now when I use something like this

Code: Select all

$date1 = date("Y-m-d G:i:s");
$date2 = "2003-09-04 21:00:00";
echo DateDiff($date1,$date2,"d, h, m, s");
It prints out something like this: -1 days, 7 hours, 51 mins, 19 secs and if I switch the $date1 and $date2 it will just count down instead and that's not exactly what I want.

I hope I made myself clear, any suggestions are appreciated :)


--lunartek

Posted: Wed Sep 03, 2003 10:05 pm
by JAM
You could replace the last line with,

Code: Select all

echo str_replace("-","",DateDiff($date1,$date2,"d, h, m, s"));
...but the 'counting' down is really correct.
What other way would you like it?

Posted: Wed Sep 03, 2003 10:36 pm
by lunartek
Yes, I've thought of your suggestion there, will have to try it but was thinking I had something missing in the calculation.

I better explain what I will be using this for. When a user visit I place a cookie on the computer with a datestamp of 86400 period (a day). This datestamp I will then use to calculate how long they have been on the page.

It should look something like this:
"you've been here 5 times in 3 hours, 30 mins."

If it will count down, then it will only show in the amount when the cookie expires, and not how long they have been there.

Posted: Thu Sep 04, 2003 3:22 am
by volka
make sure you do not subtract the greater number from the lesser

Code: Select all

$date = ($date1 > $date2) ? $date1-$date2 : $date2 - $date1;

Posted: Thu Sep 04, 2003 1:22 pm
by lunartek
I've tried replacing the "-" which only a step closer to this solution. When I use a cookie that's 300 seconds using

Code: Select all

DateDiff($mydate,$_COOKIEї'mycookie'],"d, h, m, s");
It displays this...
cookie: 1062700028
date: 1062699729
been here for 1 days, 23 hours, 55 mins, 1 secs
As you can see it's something small missing here but I'm running out of ideas now. What volka suggested didn't really work because either way it counts down and I need it to start counting from when the user first visit the page and next time they visited, which mean it must be counting up instead of down.

Posted: Thu Sep 04, 2003 5:39 pm
by volka
i've tested my version and trust me it works ;)
September 4, 2003, 8:27 pm
September 4, 2003, 8:22 pm
0 days 0 hours 4 mins 59 secs
anyway, why do you pass the parameters in that order? Isn't it always true that the current date is not less than the cookie's date? Just swap the parameters for DateDiff.

Code: Select all

$date = ($date1 > $date2) ? $date1-$date2 : $date2 - $date1;
does the same, it swaps the operands if the result is obviously nonsense (i.e. < 0)

Posted: Thu Sep 04, 2003 5:56 pm
by lunartek
I think I haven't made myself clear enough, your code works fine :) but the problem is using this code is that when you load the page it can say something like 4 mins, 50 secs.

But that's just how long it's left until the cookie expires, I want it to just show 0 mins, 10 secs and if you reload it again after 20 secs it should show 0 mins, 30 secs. I don't know if this is possible or not and that's why I tried switch the order.

I just want the user to see how long they have been there, not when the cookie expires.

Posted: Thu Sep 04, 2003 6:14 pm
by volka
then set the current date as value of the cookie at each request

Code: Select all

<?php
function DateDiff($date1,$date2,$output){
	// Calculates date differences.
	// written on sept 3, 2003 by dreamz.com designs.
	
	if (!is_numeric($date1)){ $date1 = strtotime($date1); }
	if (!is_numeric($date2)){ $date2 = strtotime($date2); }
	$date = $date1-$date2;
	
	$d = floor($date / (3600 * 24));
	$h = floor($date/3600 - ($d * 24));
	$m = floor($date/60 - ($d * 1440) - ($h * 60));
	$s = floor($date - ($d * 86400) - ($h * 3600) - ($m * 60));
	
	$output = preg_replace("/s/","$s secs",$output);
	$output = preg_replace("/d/","$d days",$output);
	$output = preg_replace("/h/","$h hours",$output);
	$output = preg_replace("/m/","$m mins",$output);
	return $output;
}

$currentDatetime = time();
setcookie ("lastAccess", $currentDatetime,time()+3600); // doesn't change the value for this request
if (isset($_COOKIE['lastAccess']))
	echo 'last access is ', DateDiff($currentDatetime, $_COOKIE['lastAccess'], 'd h m s'), ' ago';
else
	echo 'no last access recorded';
?>

Posted: Thu Sep 04, 2003 6:55 pm
by lunartek
Wow! That was easy :) thanks a lot for your help, I was looking at totally different thing. It's appreciated.