I'm trying to create a PHP function where I enter a $date
variable with this format: 03/09/2016 - 12:02.
I created a function to turn this date / time variable into a string that says the $date
variable was x days and hours ago.
Function:
$date = $r->date;
function nicetime($date)
{
if(empty($date)) {
return "Geen datum gevonden.";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
ini_set('date.timezone', 'Europe/Berlin');
$now = time('Y-m-d H:i:s');
$unix_date = $date;
// check validity of date
if(empty($unix_date)) {
return "Error.";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
$postdate = nicetime($date);
So I use $date
to insert the date. And the outcome of $postdate
is 47 years ago.
Does someone know what I'm doing wrong? Because I shouldn't get 47 years ago.