/    Sign up×
Community /Pin to ProfileBookmark

Difference between two dates

I only want the difference between the dates to be displayed in the highest unit. For example, if it has been less than a minute between the two dates, the unit should be seconds; if less than a day’s difference between the dates, then the unit should be in hours; if less than a week, then the unit should be in days. I’ve searched everywhere, but could not find a solution. Thanks.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 22.2007 — Using [url=http://www.charles-reace.com/PHP_and_MySQL/Time_Difference/]this function I created[/url], you could do:
[code=php]
<?php
/**
* array timeDiff(int $t1, int $t2)
* $t1 and $t2 must be UNIX timestamp integers, order does not matter
* returns array broken down into years/months/weeks/etc.
*/
function timeDiff($t1, $t2)
{
if($t1 > $t2)
{
$time1 = $t2;
$time2 = $t1;
}
else
{
$time1 = $t1;
$time2 = $t2;
}
$diff = array(
'years' => 0,
'months' => 0,
'weeks' => 0,
'days' => 0,
'hours' => 0,
'minutes' => 0,
'seconds' =>0
);

foreach(array('years','months','weeks','days','hours','minutes','seconds')
as $unit)
{
while(TRUE)
{
$next = strtotime("+1 $unit", $time1);
if($next < $time2)
{
$time1 = $next;
$diff[$unit]++;
}
else
{
break;
}
}
}
return($diff);
}

$diff = timeDiff(strtotime('2007-04-22 12:23:45'), strtotime('2009-07-23 15:12:00'));
$difference = "0 seconds"; // initialize just in case
foreach($diff as $unit => $value)
{
if($value)
{
if($value == 1)
{
$unit = substr($unit, 0, -1);
}
$difference = "$value $unit";
break;
}
}
echo "Time difference: $difference";
?>
[/code]
Copy linkTweet thisAlerts:
@b_hunterauthorApr 22.2007 — When I use that, I get the problem with the epoch. It returns a date from December of 1969. How do I fix that? I'm comparing the current time to a date that is being pulled from a MySQL database.
Copy linkTweet thisAlerts:
@NogDogApr 22.2007 — Are you using [url=http://www.php.net/strtotime]strtotime()[/url] to convert the date or datetime string from the DB into a UNIX timestamp integer? Or if it's a MySQL database, you could use the [url=http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp]UNIX_TIMESTAMP() function[/url] in your query.
Copy linkTweet thisAlerts:
@b_hunterauthorApr 22.2007 — Thank you. I didn't know about the TIMESTAMPDIFF that is located on that page. I think I can use that.

I'm extremely confused at this point. Is there a way that I can convert the date after getting it from the mysql database, like:

UNIXTIMESTAMP($row[timeiwanttoconvert]);
Copy linkTweet thisAlerts:
@b_hunterauthorApr 22.2007 — I used the strtotime and it worked. Thanks.
Copy linkTweet thisAlerts:
@NogDogApr 22.2007 — You're welcome. ?
×

Success!

Help @b_hunter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...