for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Spiral\Statistics;
class DatetimeConverter
{
/**
* @param \DateTimeInterface $datetime
* @param string|null $interval
* @return \DateTimeImmutable
*/
public function convert(
\DateTimeInterface $datetime,
string $interval = null
): \DateTimeImmutable
$datetime = $this->immutable($datetime);
switch ($interval) {
case 'day':
return $datetime->setTime(0, 0, 0);
case 'week':
$weekSub = $datetime->format('w') ? $datetime->format('w') - 1 : 6;
return $datetime
->sub(new \DateInterval('P' . $weekSub . 'D'))
->setTime(0, 0, 0);
case 'month':
->setDate($datetime->format('Y'), $datetime->format('m'), 1)
case 'year':
->setDate($datetime->format('Y'), 1, 1)
}
return $datetime;
public function immutable(\DateTimeInterface $datetime): \DateTimeImmutable
if ($datetime instanceof \DateTime) {
return \DateTimeImmutable::createFromMutable($datetime);