Fix time difference summaries

This commit is contained in:
Ben Goldsworthy 2022-10-21 21:20:28 +00:00
parent ee75bc6976
commit 064cf0642c

View file

@ -168,43 +168,43 @@
@php @php
function render_date_difference ($start_time) { function render_date_difference ($start_time) {
$minute = 60; $minute = 60;
$hour = 60 * 60; $hour = $minute * 60;
$day = 3600 * 24; $day = $hour * 24;
$week = 86400 * 7; $week = $day * 7;
$month = 604800 * 4; $month = $week * 4;
$year = 2629800 * 12; $year = $month * 12;
$start_time_dt = new DateTime($start_time); $start_time_dt = new DateTime($start_time);
$now = new DateTime(); $now = new DateTime();
$trip_start_difference = intval(($now->getTimestamp() - $start_time_dt->getTimestamp())); $trip_start_difference = intval(($now->getTimestamp() - $start_time_dt->getTimestamp()));
$start_tag = '<span title="' . date('j M Y (G:H)', strtotime($start_time)) . '">'; $start_tag = '<span title="' . date('G:H, j M Y', strtotime($start_time)) . '">';
$end_tag = "ago</span>"; $end_tag = '</span>';
$unit = null; $unit = null;
$div = 1; $div = 1;
if ($trip_start_difference < $minute) { if ($trip_start_difference < $minute) {
$unit = ' seconds '; $unit = 'second';
} else if ($trip_start_difference < $hour) { } else if ($trip_start_difference < $hour) {
$div = 60; $div = $minute;
$unit = ' minutes '; $unit = 'minute';
} else if ($trip_start_difference < $day) { } else if ($trip_start_difference < $day) {
$div = 3600; $div = $hour;
$unit = ' hours '; $unit = 'hour';
} else if ($trip_start_difference < $week) { } else if ($trip_start_difference < $week) {
$div = 86400; $div = $day;
$unit = ' days '; $unit = 'day';
} else if ($trip_start_difference < $month) { } else if ($trip_start_difference < $month) {
$div = 604800; $div = $week;
$unit = ' weeks '; $unit = 'week';
} else if ($trip_start_difference < $year) { } else if ($trip_start_difference < $year) {
$div = 2629800; $div = $month;
$unit = ' months '; $unit = 'month';
} else { } else {
$div = 31557600; $div = $year;
$unit = ' years '; $unit = 'year';
} }
return $start_tag . (floor( $trip_start_difference / $div )) . $unit . $end_tag; return "{$start_tag}" . ( floor ( $trip_start_difference / $div ) ) . " {$unit}" . ( ( floor ( $trip_start_difference / $div ) > 1 ) ? 's' : '' ) . " ago{$end_tag}";
} }
@endphp @endphp