Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • ArrayHash
  • ArrayList
  • Arrays
  • Callback
  • DateTime
  • FileSystem
  • Finder
  • Html
  • Image
  • Json
  • ObjectMixin
  • Paginator
  • Random
  • Reflection
  • Strings
  • TokenIterator
  • Tokenizer
  • Validators

Interfaces

  • IHtmlString

Exceptions

  • AssertionException
  • ImageException
  • JsonException
  • RegexpException
  • TokenizerException
  • UnknownImageFileException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Utils;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * DateTime.
 15:  */
 16: class DateTime extends \DateTime implements \JsonSerializable
 17: {
 18:     use Nette\SmartObject;
 19: 
 20:     /** minute in seconds */
 21:     const MINUTE = 60;
 22: 
 23:     /** hour in seconds */
 24:     const HOUR = 60 * self::MINUTE;
 25: 
 26:     /** day in seconds */
 27:     const DAY = 24 * self::HOUR;
 28: 
 29:     /** week in seconds */
 30:     const WEEK = 7 * self::DAY;
 31: 
 32:     /** average month in seconds */
 33:     const MONTH = 2629800;
 34: 
 35:     /** average year in seconds */
 36:     const YEAR = 31557600;
 37: 
 38: 
 39:     /**
 40:      * DateTime object factory.
 41:      * @param  string|int|\DateTimeInterface
 42:      * @return static
 43:      */
 44:     public static function from($time)
 45:     {
 46:         if ($time instanceof \DateTimeInterface) {
 47:             return new static($time->format('Y-m-d H:i:s.u'), $time->getTimezone());
 48: 
 49:         } elseif (is_numeric($time)) {
 50:             if ($time <= self::YEAR) {
 51:                 $time += time();
 52:             }
 53:             return (new static('@' . $time))->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
 54: 
 55:         } else { // textual or null
 56:             return new static($time);
 57:         }
 58:     }
 59: 
 60: 
 61:     /**
 62:      * Creates DateTime object.
 63:      * @return static
 64:      */
 65:     public static function fromParts($year, $month, $day, $hour = 0, $minute = 0, $second = 0)
 66:     {
 67:         $s = sprintf('%04d-%02d-%02d %02d:%02d:%02.5f', $year, $month, $day, $hour, $minute, $second);
 68:         if (!checkdate($month, $day, $year) || $hour < 0 || $hour > 23 || $minute < 0 || $minute > 59 || $second < 0 || $second >= 60) {
 69:             throw new Nette\InvalidArgumentException("Invalid date '$s'");
 70:         }
 71:         return new static($s);
 72:     }
 73: 
 74: 
 75:     /**
 76:      * @return string
 77:      */
 78:     public function __toString()
 79:     {
 80:         return $this->format('Y-m-d H:i:s');
 81:     }
 82: 
 83: 
 84:     /**
 85:      * @param  string
 86:      * @return static
 87:      */
 88:     public function modifyClone($modify = '')
 89:     {
 90:         $dolly = clone $this;
 91:         return $modify ? $dolly->modify($modify) : $dolly;
 92:     }
 93: 
 94: 
 95:     /**
 96:      * @param  int
 97:      * @return static
 98:      */
 99:     public function setTimestamp($timestamp)
100:     {
101:         $zone = $this->getTimezone();
102:         $this->__construct('@' . $timestamp);
103:         return $this->setTimeZone($zone);
104:     }
105: 
106: 
107:     /**
108:      * @return int|string
109:      */
110:     public function getTimestamp()
111:     {
112:         $ts = $this->format('U');
113:         return is_float($tmp = $ts * 1) ? $ts : $tmp;
114:     }
115: 
116: 
117:     /**
118:      * Returns new DateTime object formatted according to the specified format.
119:      * @param string The format the $time parameter should be in
120:      * @param string String representing the time
121:      * @param string|\DateTimeZone desired timezone (default timezone is used if null is passed)
122:      * @return static|false
123:      */
124:     public static function createFromFormat($format, $time, $timezone = null)
125:     {
126:         if ($timezone === null) {
127:             $timezone = new \DateTimeZone(date_default_timezone_get());
128: 
129:         } elseif (is_string($timezone)) {
130:             $timezone = new \DateTimeZone($timezone);
131: 
132:         } elseif (!$timezone instanceof \DateTimeZone) {
133:             throw new Nette\InvalidArgumentException('Invalid timezone given');
134:         }
135: 
136:         $date = parent::createFromFormat($format, $time, $timezone);
137:         return $date ? static::from($date) : false;
138:     }
139: 
140: 
141:     /**
142:      * Returns JSON representation in ISO 8601 (used by JavaScript).
143:      * @return string
144:      */
145:     public function jsonSerialize()
146:     {
147:         return $this->format('c');
148:     }
149: }
150: 
Nette 2.4-20170829 API API documentation generated by ApiGen 2.8.0