Created
July 30, 2018 06:32
-
-
Save slischka/b1a14de7715cd69409902845cb89294d to your computer and use it in GitHub Desktop.
Simple Date helper.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
namespace Fapi\Common\DateTime; | |
use SmartEmailing\Types\Duration; | |
use SmartEmailing\Types\TimeUnit; | |
class DateTimeHelpers | |
{ | |
public static function diffDays(\DateTimeInterface $first, \DateTimeInterface $second): int | |
{ | |
$first = new \DateTime($first->format('Y-m-d') . ' 00:00:00'); | |
$second = new \DateTime($second->format('Y-m-d') . ' 00:00:00'); | |
$diff = $second->diff($first); | |
return $diff->days * ($diff->invert ? -1 : 1); | |
} | |
public static function getDateRangeLength(\DateTimeInterface $begin, \DateTimeInterface $end): int | |
{ | |
$diff = static::diffDays($end, $begin); | |
return $diff >= 0 ? $diff + 1 : 0; | |
} | |
public static function addDays(\DateTimeInterface $date, int $days): \DateTime | |
{ | |
$date = new \DateTime($date->format('Y-m-d H:i:s')); | |
return $date->modify($days . ' days'); | |
} | |
public static function addMonths(\DateTimeInterface $date, int $months): \DateTime | |
{ | |
$date = new \DateTime($date->format('Y-m-d H:i:s')); | |
return $date->modify($months . ' months'); | |
} | |
public static function setDayOfMonth(\DateTimeInterface $date, int $day): \DateTime | |
{ | |
return static::addDays($date, $day - (int) $date->format('j')); | |
} | |
public static function isBetweenOrEqual(\DateTimeInterface $date, \DateTimeInterface $beginDate, \DateTimeInterface $endDate): bool | |
{ | |
return self::isGreaterOrEqual($date, $beginDate) && | |
self::isLowerOrEqual($date, $endDate); | |
} | |
public static function isGreaterOrEqual(\DateTimeInterface $first, \DateTimeInterface $second): bool | |
{ | |
return self::diffDays($first, $second) >= 0; | |
} | |
public static function isLowerOrEqual(\DateTimeInterface $first, \DateTimeInterface $second): bool | |
{ | |
return self::diffDays($first, $second) <= 0; | |
} | |
public static function isLower(\DateTimeInterface $first, \DateTimeInterface $second): bool | |
{ | |
return self::diffDays($first, $second) < 0; | |
} | |
public static function isGreater(\DateTimeInterface $first, \DateTimeInterface $second): bool | |
{ | |
return self::diffDays($first, $second) > 0; | |
} | |
public static function formatDateTime(\DateTimeInterface $dateTime): string | |
{ | |
return $dateTime->format('Y-m-d H:i:s'); | |
} | |
public static function setDayOfNextMonth(\DateTime $date, int $day): \DateTime | |
{ | |
$month = (int) $date->format('m'); | |
$year = (int) $date->format('Y'); | |
$month++; | |
if ($month === 13) { | |
$year++; | |
$month = 1; | |
} | |
$date->setDate($year, $month, 1); | |
return self::setDayOrLastDayOfMonth($date, $day); | |
} | |
public static function modifyByDurationForPeriodicInvoice(\DateTime $date, Duration $duration, ?int $day): \DateTime | |
{ | |
if ($day && $duration->getUnit()->equalsValue(TimeUnit::MONTHS)) { | |
for ($i = 0; $i < $duration->getValue(); $i++) { | |
$date = self::setDayOfNextMonth($date, $day); | |
} | |
return $date; | |
} | |
return $date->modify($duration->getDateTimeModify()); | |
} | |
public static function setDayOrLastDayOfMonth(\DateTime $date, int $day): \DateTime | |
{ | |
$month = (int) $date->format('m'); | |
$year = (int) $date->format('Y'); | |
$lastDayOfMonth = (int) $date->format('t'); | |
if ($day > $lastDayOfMonth) { | |
$day = $lastDayOfMonth; | |
} | |
$date->setDate($year, $month, $day); | |
return $date; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment