Reputation: 12806
I have two timestamps provided by time() and stored in database as varchar.
Now I want to calculate the difference between two times; one is 1366627990
, 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 of 10 days
.
Is there something wrong with my script?
Upvotes: 0
Views: 2075
Reputation: 18446
First of all, don't expect the difference to be less than 10 days. It's 31 years:
echo date("r", 366627990); // your first time. Fri, 14 Aug 1981 09:06:30 +0000
echo date("r", 1366971706); // $time2 in my case. Fri, 26 Apr 2013 10:21:46 +0000
Okay, now you've corrected the date to 1366627990
. That's a difference of 4 days:
echo date("r", 1366627990); // updated time. Mon, 22 Apr 2013 10:53:10 +0000
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.
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:
function dateDiff($time1, $time2, $precision = 3) {
// hack for passing timestamps as strings
if ($time1 == intval($time1)) {
$time1 = intval($time1);
}
if ($time2 == intval($time2)) {
$time2 = intval($time2);
}
Now, your function yields the correct output:
31 years, 8 months, 12 days
As of version 5.2.0, PHP has the super-neat DateTime
class with a diff method, which is exactly what you need:
$d1 = new DateTime();
$d1->SetTimestamp($time1);
$d2 = new DateTime();
$diff = $d1->diff($d2);
echo $diff->format("%y years, %m months, %d days");
This one also gives you
31 years, 8 months, 12 days
Upvotes: 2
Reputation: 7315
Try
<?php
$time1 = (int)1366971673; //The value of time() when I wrote this.
$time2 = (int)1366627990; //Your time value to compare.
$diff = (int)$time1-$time2;
echo round(($diff/60/60/24)).' days between dates.';
?>
Should output;
4 days between dates.
http://phpfiddle.org/main/code/ahc-mg3
Upvotes: 0
Reputation: 455
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
Upvotes: 3