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

54 lines
1.6 KiB
PHP
Raw Normal View History

2022-11-01 04:06:31 +00:00
<?php
if (!function_exists('render_date_difference')) {
2022-11-30 18:47:09 +00:00
function render_date_difference($start_time)
{
2022-11-01 04:06:31 +00:00
$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';
2022-11-30 18:47:09 +00:00
} elseif ($trip_start_difference < $hour) {
2022-11-01 04:06:31 +00:00
$div = $minute;
$unit = 'minute';
2022-11-30 18:47:09 +00:00
} elseif ($trip_start_difference < $day) {
2022-11-01 04:06:31 +00:00
$div = $hour;
$unit = 'hour';
2022-11-30 18:47:09 +00:00
} elseif ($trip_start_difference < $week) {
2022-11-01 04:06:31 +00:00
$div = $day;
$unit = 'day';
2022-11-30 18:47:09 +00:00
} elseif ($trip_start_difference < $month) {
2022-11-01 04:06:31 +00:00
$div = $week;
$unit = 'week';
2022-11-30 18:47:09 +00:00
} elseif ($trip_start_difference < $year) {
2022-11-01 04:06:31 +00:00
$div = $month;
$unit = 'month';
} else {
$div = $year;
$unit = 'year';
}
2022-11-30 18:47:09 +00:00
return $start_tag .
( floor($trip_start_difference / $div) ) .
" {$unit}" .
(
( floor($trip_start_difference / $div) > 1 ) ?
's' :
''
) .
" ago{$end_tag}";
2022-11-01 04:06:31 +00:00
}
}