Where-in-the-World-is-Ben/app/helper.php

54 lines
1.6 KiB
PHP

<?php
if (!function_exists('render_date_difference')) {
function render_date_difference($start_time)
{
$minute = 60;
$hour = $minute * 60;
$day = $hour * 24;
$week = $day * 7;
$month = $week * 4;
$year = $month * 12;
$start_time_dt = new DateTime($start_time);
$now = new DateTime();
$trip_start_difference = intval(($now->getTimestamp() - $start_time_dt->getTimestamp()));
$start_tag = '<span title="' . date('G:H, j M Y', strtotime($start_time)) . '">';
$end_tag = '</span>';
$unit = null;
$div = 1;
if ($trip_start_difference < $minute) {
$unit = 'second';
} elseif ($trip_start_difference < $hour) {
$div = $minute;
$unit = 'minute';
} elseif ($trip_start_difference < $day) {
$div = $hour;
$unit = 'hour';
} elseif ($trip_start_difference < $week) {
$div = $day;
$unit = 'day';
} elseif ($trip_start_difference < $month) {
$div = $week;
$unit = 'week';
} elseif ($trip_start_difference < $year) {
$div = $month;
$unit = 'month';
} else {
$div = $year;
$unit = 'year';
}
return $start_tag .
( floor($trip_start_difference / $div) ) .
" {$unit}" .
(
( floor($trip_start_difference / $div) > 1 ) ?
's' :
''
) .
" ago{$end_tag}";
}
}