Skip to content

Instantly share code, notes, and snippets.

@lucasnpinheiro
Forked from lstrojny/DateTime.php
Created April 5, 2016 02:09
Show Gist options
  • Select an option

  • Save lucasnpinheiro/d6861171f09ca9fe624d7229ecd23b0c to your computer and use it in GitHub Desktop.

Select an option

Save lucasnpinheiro/d6861171f09ca9fe624d7229ecd23b0c to your computer and use it in GitHub Desktop.

Revisions

  1. @lstrojny lstrojny renamed this gist Feb 5, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @lstrojny lstrojny created this gist Feb 5, 2012.
    118 changes: 118 additions & 0 deletions DateTime
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,118 @@
    <?php

    namespace InterNations\Component\Date\Value;

    use DateTime as DateTimeBase;
    use DateTimeZone;
    use DateInterval;

    class DateTime extends DateTimeBase
    {
    protected $frozen = true;

    public function __construct($dateString = null, DateTimeZone $timezone = null)
    {
    if ($timezone) {
    parent::__construct($dateString, $timezone);
    } else {
    parent::__construct($dateString);
    }
    }

    public static function now($microtime = false, DateTimeZone $timezone = null)
    {
    if ($microtime) {
    return new static(date('Y-m-d H:i:s' . substr(microtime(true), 10)), $timezone);
    }
    return new static('now', $timezone);
    }

    public static function createFromFormat($format, $time, $timezone = null)
    {
    if ($timezone) {
    $date = parent::createFromFormat($format, $time, $timezone);
    } else {
    $date = parent::createFromFormat($format, $time);
    }

    if (false === $date) {
    return false;
    }

    return new static($date->format('Y-m-d H:i:s.u'), $timezone);
    }

    public function modify($dateString)
    {
    return $this->_guard(__FUNCTION__, array($dateString));
    }

    public function add($interval)
    {
    return $this->_guard('add', func_get_args());
    }

    public function sub($interval)
    {
    return $this->_guard('sub', func_get_args());
    }

    public function setTimezone($timezone)
    {
    return $this->_guard('setTimezone', func_get_args());
    }

    public function setTime($hour, $minute, $second = null)
    {
    return $this->_guard('setTime', func_get_args());
    }

    public function setDate($year, $month, $day)
    {
    return $this->_guard('setDate', func_get_args());
    }

    public function setISODate($year, $week, $day = 1)
    {
    return $this->_guard('setISODate', func_get_args());
    }

    public function setTimestamp($timestamp)
    {
    return $this->_guard('setTimestamp', func_get_args());
    }

    public function diff($date, $absolute = false)
    {
    return $this->_guard('diff', func_get_args());
    }

    public function isFuture(DateTime $compareDate = null)
    {
    if (!$compareDate) {
    $compareDate = self::now(false, $this->getTimezone());
    }
    return $this > $compareDate;
    }

    public function isPast(DateTime $compareDate = null)
    {
    if (!$compareDate) {
    $compareDate = self::now(false, $this->getTimezone());
    }
    return $this < $compareDate;
    }

    protected function _guard($method, array $arguments)
    {
    if ($this->frozen) {
    $date = clone $this;
    $date->frozen = false;
    call_user_func_array(array($date, $method), $arguments);
    $date->frozen = true;
    return $date;
    } else {
    return call_user_func_array(array('parent', $method), $arguments);
    }
    }
    }