Create helpers file

This commit is contained in:
Ben Goldsworthy 2022-11-01 04:06:31 +00:00
parent 9956dbb5d3
commit 940fa5fda4
4 changed files with 523 additions and 389 deletions

44
app/helper.php Normal file
View file

@ -0,0 +1,44 @@
<?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';
} else if ($trip_start_difference < $hour) {
$div = $minute;
$unit = 'minute';
} else if ($trip_start_difference < $day) {
$div = $hour;
$unit = 'hour';
} else if ($trip_start_difference < $week) {
$div = $day;
$unit = 'day';
} else if ($trip_start_difference < $month) {
$div = $week;
$unit = 'week';
} else if ($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}";
}
}

View file

@ -28,7 +28,10 @@
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"files": [
"app/helper.php"
]
},
"autoload-dev": {
"psr-4": {

819
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -169,47 +169,3 @@
</footer>
</body>
</html>
@php
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';
} else if ($trip_start_difference < $hour) {
$div = $minute;
$unit = 'minute';
} else if ($trip_start_difference < $day) {
$div = $hour;
$unit = 'hour';
} else if ($trip_start_difference < $week) {
$div = $day;
$unit = 'day';
} else if ($trip_start_difference < $month) {
$div = $week;
$unit = 'week';
} else if ($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}";
}
@endphp