ICanBoogie/DateTime 2.0.x
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • DateTime

Classes

  • DateTimeLocalizer
  • ImmutableDateTime
  • MutableDateTime
  • TimeZone
  • TimeZoneLocation

Interfaces

  • DateTime
  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 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie;

/**
 * @property int $year Year.
 * @property int $month Month of the year.
 * @property int $day Day of the month.
 * @property int $hour Hour of the day.
 * @property int $minute Minute of the hour.
 * @property int $second Second of the minute.
 * @property int $timestamp Unix timestamp.
 *
 * @property-read MutableDateTime $tomorrow A new instance representing the next day. Time is reset to 00:00:00.
 * @property-read MutableDateTime $yesterday A new instance representing the previous day. Time is reset to 00:00:00.
 * @property-read MutableDateTime $monday A new instance representing Monday of the week. Time is reset to 00:00:00.
 * @property-read MutableDateTime $tuesday A new instance representing Tuesday of the week. Time is reset to 00:00:00.
 * @property-read MutableDateTime $wednesday A new instance representing Wednesday of the week. Time is reset to 00:00:00.
 * @property-read MutableDateTime $thursday A new instance representing Thursday of the week. Time is reset to 00:00:00.
 * @property-read MutableDateTime $friday A new instance representing Friday of the week. Time is reset to 00:00:00.
 * @property-read MutableDateTime $saturday A new instance representing Saturday of the week. Time is reset to 00:00:00.
 * @property-read MutableDateTime $sunday A new instance representing Sunday of the week. Time is reset to 00:00:00.
 *
 * @property TimeZone $timezone The timezone of the instance.
 * @property TimeZone $tz The timezone of the instance.
 * @property-read MutableDateTime $utc A new instance in the UTC timezone.
 * @property-read MutableDateTime $local A new instance in the local timezone.
 *
 * @method $this change(array $options, $cascade = false)
 */
class MutableDateTime extends \DateTime implements \JsonSerializable, DateTime
{
    use DateTime\Shared;
    use DateTime\Readers;

    const ISO8601 = 'Y-m-d\TH:i:sP';

    /**
     * @inheritdoc
     */
    static public function now()
    {
        return empty($_SERVER['REQUEST_TIME']) ? new static : (new static('@' . $_SERVER['REQUEST_TIME']))->local;
    }

    /**
     * @return MutableDateTime
     */
    protected function get_local()
    {
        $time = clone $this;
        $time->setTimezone(date_default_timezone_get());

        return $time;
    }

    /**
     * @return MutableDateTime
     */
    protected function get_tomorrow()
    {
        $time = clone $this;
        $time->modify('+1 day');
        $time->setTime(0, 0, 0);

        return $time;
    }

    /**
     * @return MutableDateTime
     */
    protected function get_yesterday()
    {
        $time = clone $this;
        $time->modify('-1 day');
        $time->setTime(0, 0, 0);

        return $time;
    }

    /**
     * Sets the {@link $year}, {@link $month}, {@link $day}, {@link $hour}, {@link $minute},
     * {@link $second}, {@link $timestamp} and {@link $zone} properties.
     *
     * @throws \LogicException in attempt to set a read-only or undefined property.
     *
     * @inheritdoc
     */
    public function __set($property, $value)
    {
        static $readonly = [ 'quarter', 'week', 'year_day', 'weekday', 'tomorrow', 'yesterday', 'utc', 'local' ];

        switch ($property)
        {
            case 'year':
            case 'month':
            case 'day':
            case 'hour':
            case 'minute':
            case 'second':
                $this->change([ $property => $value ]);
                return;

            case 'timestamp':
                $this->setTimestamp($value);
                return;

            case 'timezone':
            case 'tz':
                $this->setTimezone($value);
                return;
        }

        if (strpos($property, 'is_') === 0 || strpos($property, 'as_') === 0 || in_array($property, $readonly) || method_exists($this, 'get_' . $property))
        {
            throw new \LogicException("Property is not writable: $property.");
        }

        throw new \LogicException("Property is not defined: $property.");
    }

    /**
     * @inheritdoc
     */
    public function with(array $options, $cascade = false)
    {
        $dt = clone $this;

        return $dt->change($options, $cascade);
    }
}
ICanBoogie/DateTime 2.0.x API documentation generated by ApiGen