vendor/thecodingmachine/safe/lib/DateTime.php line 11

Open in your IDE?
  1. <?php
  2. namespace Safe;
  3. use DateInterval;
  4. use DateTimeInterface;
  5. use DateTimeZone;
  6. use Safe\Exceptions\DatetimeException;
  7. /** this class implements a safe version of the Datetime class */
  8. class DateTime extends \DateTime
  9. {
  10.     //switch from regular datetime to safe version
  11.     private static function createFromRegular(\DateTime $datetime): self
  12.     {
  13.         return new self($datetime->format('Y-m-d H:i:s.u'), $datetime->getTimezone());
  14.     }
  15.     /**
  16.      * @param string $format
  17.      * @param string $time
  18.      * @param DateTimeZone|null $timezone
  19.      * @throws DatetimeException
  20.      */
  21.     public static function createFromFormat($format$time$timezone null): self
  22.     {
  23.         $datetime parent::createFromFormat($format$time$timezone);
  24.         if ($datetime === false) {
  25.             throw DatetimeException::createFromPhpError();
  26.         }
  27.         return self::createFromRegular($datetime);
  28.     }
  29.     /**
  30.      * @param DateTimeInterface $datetime2 The date to compare to.
  31.      * @param boolean $absolute [optional] Whether to return absolute difference.
  32.      * @return DateInterval The DateInterval object representing the difference between the two dates.
  33.      * @throws DatetimeException
  34.      */
  35.     public function diff($datetime2$absolute false): DateInterval
  36.     {
  37.         /** @var \DateInterval|false $result */
  38.         $result parent::diff($datetime2$absolute);
  39.         if ($result === false) {
  40.             throw DatetimeException::createFromPhpError();
  41.         }
  42.         return $result;
  43.     }
  44.     /**
  45.      * @param string $modify A date/time string. Valid formats are explained in <a href="https://secure.php.net/manual/en/datetime.formats.php">Date and Time Formats</a>.
  46.      * @return DateTime Returns the DateTime object for method chaining.
  47.      * @throws DatetimeException
  48.      */
  49.     public function modify($modify): self
  50.     {
  51.         /** @var DateTime|false $result */
  52.         $result parent::modify($modify);
  53.         if ($result === false) {
  54.             throw DatetimeException::createFromPhpError();
  55.         }
  56.         return $result;
  57.     }
  58.     /**
  59.      * @param int $year
  60.      * @param int $month
  61.      * @param int $day
  62.      * @return DateTime
  63.      * @throws DatetimeException
  64.      */
  65.     public function setDate($year$month$day): self
  66.     {
  67.         /** @var DateTime|false $result */
  68.         $result parent::setDate($year$month$day);
  69.         if ($result === false) {
  70.             throw DatetimeException::createFromPhpError();
  71.         }
  72.         return $result;
  73.     }
  74. }