Spaces:
No application file
No application file
File size: 6,546 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
<?php
namespace Mautic\CoreBundle\Twig\Helper;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\DateTimeHelper;
use Symfony\Contracts\Translation\TranslatorInterface;
final class DateHelper
{
/**
* @var string[]
*/
private array $formats;
private DateTimeHelper $helper;
/**
* @param string $dateFullFormat
* @param string $dateShortFormat
* @param string $dateOnlyFormat
* @param string $timeOnlyFormat
*/
public function __construct(
$dateFullFormat,
$dateShortFormat,
$dateOnlyFormat,
$timeOnlyFormat,
private TranslatorInterface $translator,
private CoreParametersHelper $coreParametersHelper
) {
$this->formats = [
'datetime' => $dateFullFormat,
'short' => $dateShortFormat,
'date' => $dateOnlyFormat,
'time' => $timeOnlyFormat,
];
$this->helper = new DateTimeHelper('', 'Y-m-d H:i:s', 'local');
}
/**
* @param string $type
* @param \DateTime|string $datetime
* @param string $timezone
* @param string $fromFormat
*
* @return string
*/
private function format($type, $datetime, $timezone, $fromFormat)
{
if (empty($datetime)) {
return '';
} else {
$this->helper->setDateTime($datetime, $fromFormat, $timezone);
return $this->helper->toLocalString(
$this->formats[$type]
);
}
}
/**
* Returns full date. eg. October 8, 2014 21:19.
*
* @param \DateTime|string $datetime
* @param string $timezone
* @param string $fromFormat
*
* @return string
*/
public function toFull($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s')
{
return $this->format('datetime', $datetime, $timezone, $fromFormat);
}
/**
* Returns date and time concat eg 2014-08-02 5:00am.
*
* @param \DateTime|string $datetime
* @param string $timezone
* @param string $fromFormat
*
* @return string
*/
public function toFullConcat($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s')
{
$this->helper->setDateTime($datetime, $fromFormat, $timezone);
return $this->helper->toLocalString(
$this->formats['date'].' '.$this->formats['time']
);
}
/**
* Returns short date format eg Sun, Oct 8.
*
* @param \DateTime|string $datetime
* @param string $timezone
* @param string $fromFormat
*
* @return string
*/
public function toShort($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s')
{
return $this->format('short', $datetime, $timezone, $fromFormat);
}
/**
* Returns date only e.g. 2014-08-09.
*
* @param \DateTime|string $datetime
* @param string $timezone
* @param string $fromFormat
*
* @return string
*/
public function toDate($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s')
{
return $this->format('date', $datetime, $timezone, $fromFormat);
}
/**
* Returns time only e.g. 21:19.
*
* @param \DateTime|string $datetime
* @param string $timezone
* @param string $fromFormat
*
* @return string
*/
public function toTime($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s')
{
return $this->format('time', $datetime, $timezone, $fromFormat);
}
/**
* Returns date/time like Today, 10:00 AM.
*
* @param string|int<min, -1>|int<1, max>|\DateTime $datetime
* @param string $timezone
* @param string $fromFormat
* @param bool $forceDateForNonText If true, return as full date/time rather than "29 days ago"
*/
public function toText($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s', $forceDateForNonText = false): string
{
if (empty($datetime)) {
return '';
}
$this->helper->setDateTime($datetime, $fromFormat, $timezone);
$textDate = $this->helper->getTextDate();
$dt = $this->helper->getLocalDateTime();
if ($textDate) {
return $this->translator->trans('mautic.core.date.'.$textDate, ['%time%' => $dt->format($this->coreParametersHelper->get('date_format_timeonly'))]);
} else {
$interval = $this->helper->getDiff('now', null, true);
if ($interval->invert && !$forceDateForNonText) {
// In the past
return $this->translator->trans('mautic.core.date.ago', ['%days%' => $interval->days]);
} else {
// In the future
return $this->toFullConcat($datetime, $timezone, $fromFormat);
}
}
}
/**
* Format DateInterval into humanly readable format.
* Example: 55 minutes 49 seconds.
* It doesn't return zero values like 0 years.
*/
public function formatRange(\DateInterval $range): string
{
$formated = [];
$timeUnits = ['y' => 'year', 'm' => 'month', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second'];
foreach ($timeUnits as $key => $unit) {
if ($range->{$key}) {
$formated[] = $this->translator->trans(
'mautic.core.date.'.$unit,
['%count%' => $range->{$key}]
);
}
}
if (empty($formated)) {
return $this->translator->trans('mautic.core.date.less.than.second');
}
return implode(' ', $formated);
}
/**
* @return string
*/
public function getFullFormat()
{
return $this->formats['datetime'];
}
/**
* @return string
*/
public function getDateFormat()
{
return $this->formats['date'];
}
/**
* @return string
*/
public function getTimeFormat()
{
return $this->formats['time'];
}
/**
* @return string
*/
public function getShortFormat()
{
return $this->formats['short'];
}
public function getName(): string
{
return 'date';
}
}
|