php - Time calculation between two timestamps
I have two timestamps provided by time() and stored in database as varchar.
Now I want to calculate the difference between two times; one is1366627990
, and the other one$time2=time();
.
I used a script
function dateDiff($time1, $time2, $precision = 3) {
date_default_timezone_set("UTC");
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($time2)) {
$time2 = strtotime($time2);
}
// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;
}
// Set up intervals and diffs arrays
$intervals = array('year','month','day','hour','minute','second');
$diffs = array();
// Loop thru all intervals
foreach ($intervals as $interval) {
// Set default diff to 0
$diffs[$interval] = 0;
// Create temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
$time1 = $ttime;
$diffs[$interval]++;
// Create new temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
}
}
$count = 0;
$times = array();
// Loop thru all diffs
foreach ($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
break;
}
// Add value and interval
// if value is bigger than 0
if ($value > 0) {
// Add s if value is not 1
if ($value != 1) {
$interval .= "s";
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
}
}
// Return string with times
return implode(", ", $times);
}
and I call the function as
echo dateDiff("1366627990",$time2, 3);
but it's giving me
43 years, 3 months, 22 days
while the time should not exceed maximum of10 days
.
Is there something wrong with my script?
Answer
Solution:
Well, first just grab the intval() of the two timestamps, subtract one from the other and take the absolute value of the result. You'll end up with the total number of seconds between the two times.
Then use one of these examples to convert that number (of seconds) to something human readable:
seconds to minutes and days to weeks
Answer
Solution:
First of all, don't expect the difference to be less than 10 days. It's 31 years:
Update:
Okay, now you've corrected the date to
1366627990
. That's a difference of 4 days:But the solutions provided in this answer are still correct and are valid for your old and new timestamp (as well as for any other two timestamps :-) ).
Now let me get to the problem in your script and then show you a much better alternative for calculating time and date differences.
Fixing your script
The problem is your "If not numeric then convert texts to unix timestamps" check in the beginning. Because this won't work if you pass UNIX timestamps as strings, which you apparently do. A hack that fixes this would be to include the following at the beginning of the function:
Now, your function yields the correct output:
A better way for calculating time intervals
As of version 5.2.0, PHP has the super-neat
with a diff method, which is exactly what you need:
This one also gives you
Answer
Solution:
Try
Should output;
4 days between dates.
http://phpfiddle.org/main/code/ahc-mg3