amdp3-metaforums/Application/Foundations/DateHelper.php

36 lines
1.0 KiB
PHP
Raw Normal View History

2019-11-19 03:36:39 +00:00
<?php
namespace Application\Foundations;
class DateHelper {
public function elapsedString($date) {
$unixTimestamp = strtotime($date);
$now = time();
$dateDiff = $now - $unixTimestamp;
$measure = "";
$ago = "";
if($dateDiff > 0) {
$ago .= " ago";
} else if($dateDiff < 0) {
$ago .= " later";
}
$dateDiff = abs($dateDiff);
$seconds = $dateDiff;
$minutes = intval($dateDiff / 60);
$hours = intval($dateDiff / 3600);
$days = intval($dateDiff / 86400);
$years = intval($dateDiff / (365 * 86400));
if($years > 0) {
$measure = $years." years";
} else if($days > 0) {
$measure = $days." days";
} else if($hours > 0) {
$measure = $hours." hours";
} else if($minutes > 0) {
$measure = $minutes." minutes";
} else if($seconds > 0) {
$measure = $seconds." seconds";
}
return $measure.$ago;
}
}