Spaces:
No application file
No application file
File size: 1,183 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php
namespace Mautic\CoreBundle\Helper\Chart;
trait DateRangeUnitTrait
{
/**
* Returns appropriate time unit from a date range so the line/bar charts won't be too full/empty.
*/
public function getTimeUnitFromDateRange(\DateTimeInterface $dateFrom, \DateTimeInterface $dateTo): string
{
$dayDiff = $dateTo->diff($dateFrom)->format('%a');
$unit = 'd';
if ($dayDiff <= 1) {
$unit = 'H';
$sameDay = $dateTo->format('d') === $dateFrom->format('d');
$hourDiff = $dateTo->diff($dateFrom)->format('%h');
$minuteDiff = $dateTo->diff($dateFrom)->format('%i');
if ($sameDay && !intval($hourDiff) && intval($minuteDiff)) {
$unit = 'i';
}
$secondDiff = $dateTo->diff($dateFrom)->format('%s');
if (!intval($minuteDiff) && intval($secondDiff)) {
$unit = 'i';
}
}
if ($dayDiff > 31) {
$unit = 'W';
}
if ($dayDiff > 100) {
$unit = 'm';
}
if ($dayDiff > 1000) {
$unit = 'Y';
}
return $unit;
}
}
|