Reputation:
I'm calculating the difference between the date in the database and at the current time. I use this a function called TimeAgo()
in my PHP code.
TimeAgo()
code:
function TimeAgo ($oldTime, $newTime)
{
$timeCalc = strtotime($newTime) - strtotime($oldTime);
if ($timeCalc >= (60*60*24*30*12*2)){
$timeCalc = intval($timeCalc/60/60/24/30/12) . " years ago";
}else if ($timeCalc >= (60*60*24*30*12)){
$timeCalc = intval($timeCalc/60/60/24/30/12) . " year ago";
}else if ($timeCalc >= (60*60*24*30*2)){
$timeCalc = intval($timeCalc/60/60/24/30) . " months ago";
}else if ($timeCalc >= (60*60*24*30)){
$timeCalc = intval($timeCalc/60/60/24/30) . " month ago";
}else if ($timeCalc >= (60*60*24*2)){
$timeCalc = intval($timeCalc/60/60/24) . " days ago";
}else if ($timeCalc >= (60*60*24)){
$timeCalc = " Yesterday";
}else if ($timeCalc >= (60*60*2)){
$timeCalc = intval($timeCalc/60/60) . " hours ago";
}else if ($timeCalc >= (60*60)){
$timeCalc = intval($timeCalc/60/60) . " hour ago";
}else if ($timeCalc >= 60*2){
$timeCalc = intval($timeCalc/60) . " minutes ago";
}else if ($timeCalc >= 60){
$timeCalc = intval($timeCalc/60) . " minute ago";
}else if ($timeCalc > 0){
$timeCalc .= " seconds ago";
}else if ($timeCalc == 0){
$timeCalc = "Just now";
}
return $timeCalc;
}
Everything works correctly, until I mess around the values in the database. If I set the date to 1901-01-01 00:00:00
it should return the result as 116 years ago
but instead it shows as 48 years ago
.
I'm using this code to show the output:
echo TimeAgo($db_user['far_date'], date("Y-m-d H:i:s"))
Is there something simple I'm missing when PHP calculates datetimes?
Upvotes: 1
Views: 52
Reputation: 40481
I think it depends on the PHP
version used. I found online :
However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).
So, this default date occurred 48 years ago , which correspond with what you got.
On newer versions the range is : Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT.
If you really want to use this date, then I advise you to do it on MySQL directly , which as I recall using ranges from year 1000 .
Upvotes: 2