/** * Updates the Date and Time. * * @param DateTime $dt * @param int $dateType * @return void */ public function setDateTime(DateTime $dt, $dateType = self::LOCALTZ) { switch ($dateType) { case self::LOCAL: $this->setValue($dt->format('Ymd\\THis')); $this->offsetUnset('VALUE'); $this->offsetUnset('TZID'); $this->offsetSet('VALUE', 'DATE-TIME'); break; case self::UTC: $dt->setTimeZone(new DateTimeZone('UTC')); $this->setValue($dt->format('Ymd\\THis\\Z')); $this->offsetUnset('VALUE'); $this->offsetUnset('TZID'); $this->offsetSet('VALUE', 'DATE-TIME'); break; case self::LOCALTZ: $this->setValue($dt->format('Ymd\\THis')); $this->offsetUnset('VALUE'); $this->offsetUnset('TZID'); $this->offsetSet('VALUE', 'DATE-TIME'); $this->offsetSet('TZID', $dt->getTimeZone()->getName()); break; case self::DATE: $this->setValue($dt->format('Ymd')); $this->offsetUnset('VALUE'); $this->offsetUnset('TZID'); $this->offsetSet('VALUE', 'DATE'); break; default: throw new InvalidArgumentException('You must pass a valid dateType constant'); } $this->dateTime = $dt; $this->dateType = $dateType; }
function formatted_time($datetime_str = 'now', $timestamp_format = NULL, $timezone = NULL) { $tz = new DateTimeZone($timezone ? $timezone : date_default_timezone_get()); $time = new DateTime($datetime_str, $tz); if ($time->getTimeZone()->getName() !== $tz->getName()) { $time->setTimeZone($tz); } return $time->format($timestamp_format); }
/** * Assert that two dates are equal. */ protected function assertDatesEqual(DateTime $dt1, DateTime $dt2, $msg = "Expected DateTime1 == DateTime2: %s") { if ($dt1 != $dt2) { if ($dt1->getTimeZone()->getName() != $dt2->getTimeZone()->getName()) { $this->fail(sprintf($msg, "Timezones were not the same.")); } else { $this->fail(sprintf($msg, "Timezones were the same, but date values were different.")); } } }
/** * Sets the property as a DateTime object. * * @param \DateTime $dt * @return void */ public function setDateTime(\DateTime $dt) { $values = array(); $tz = null; $isUtc = false; $tz = $dt->getTimeZone(); $isUtc = in_array($tz->getName(), array('UTC', 'GMT', 'Z')); if ($isUtc) { $value = $dt->format('Ymd\\THis\\Z'); } else { // Calculating the offset. $value = $dt->format('Ymd\\THisO'); } $this->value = $value; }
<?php ini_set("intl.error_level", E_WARNING); ini_set("intl.default_locale", "nl"); date_default_timezone_set('Europe/Amsterdam'); $intlcal = new IntlGregorianCalendar(); $pstdate = new DateTime('2012-01-01 00:00:00 WEST'); $intlcal->setTimeZone($pstdate->getTimeZone()); var_dump($intlcal->getTimeZone()->getID()); $pstdate = new DateTime('2012-01-01 00:00:00 +24:00'); $intlcal->setTimeZone($pstdate->getTimeZone()); var_dump($intlcal->getTimeZone()->getID());
/** * Creates the iterator * * You should pass a VCALENDAR component, as well as the UID of the event * we're going to traverse. * * @param Sabre_VObject_Component $comp */ public function __construct(Sabre_VObject_Component $vcal, $uid = null) { if (is_null($uid)) { if ($vcal->name === 'VCALENDAR') { throw new InvalidArgumentException('If you pass a VCALENDAR object, you must pass a uid argument as well'); } $components = array($vcal); $uid = (string) $vcal->uid; } else { $components = $vcal->select('VEVENT'); } foreach ($components as $component) { if ((string) $component->uid == $uid) { if (isset($component->{'RECURRENCE-ID'})) { $this->overriddenEvents[$component->DTSTART->getDateTime()->getTimeStamp()] = $component; $this->overriddenDates[] = $component->{'RECURRENCE-ID'}->getDateTime(); } else { $this->baseEvent = $component; } } } if (!$this->baseEvent) { throw new InvalidArgumentException('Could not find a base event with uid: ' . $uid); } $this->startDate = clone $this->baseEvent->DTSTART->getDateTime(); $this->endDate = null; if (isset($this->baseEvent->DTEND)) { $this->endDate = clone $this->baseEvent->DTEND->getDateTime(); } else { $this->endDate = clone $this->startDate; if (isset($this->baseEvent->DURATION)) { $this->endDate->add(Sabre_VObject_DateTimeParser::parse($this->baseEvent->DURATION->value)); } } $this->currentDate = clone $this->startDate; $rrule = (string) $this->baseEvent->RRULE; $parts = explode(';', $rrule); foreach ($parts as $part) { list($key, $value) = explode('=', $part, 2); switch (strtoupper($key)) { case 'FREQ': if (!in_array(strtolower($value), array('secondly', 'minutely', 'hourly', 'daily', 'weekly', 'monthly', 'yearly'))) { throw new InvalidArgumentException('Unknown value for FREQ=' . strtoupper($value)); } $this->frequency = strtolower($value); break; case 'UNTIL': $this->until = Sabre_VObject_DateTimeParser::parse($value); break; case 'COUNT': $this->count = (int) $value; break; case 'INTERVAL': $this->interval = (int) $value; break; case 'BYSECOND': $this->bySecond = explode(',', $value); break; case 'BYMINUTE': $this->byMinute = explode(',', $value); break; case 'BYHOUR': $this->byHour = explode(',', $value); break; case 'BYDAY': $this->byDay = explode(',', strtoupper($value)); break; case 'BYMONTHDAY': $this->byMonthDay = explode(',', $value); break; case 'BYYEARDAY': $this->byYearDay = explode(',', $value); break; case 'BYWEEKNO': $this->byWeekNo = explode(',', $value); break; case 'BYMONTH': $this->byMonth = explode(',', $value); break; case 'BYSETPOS': $this->bySetPos = explode(',', $value); break; case 'WKST': $this->weekStart = strtoupper($value); break; } } // Parsing exception dates if (isset($this->baseEvent->EXDATE)) { foreach ($this->baseEvent->EXDATE as $exDate) { foreach (explode(',', (string) $exDate) as $exceptionDate) { $this->exceptionDates[] = Sabre_VObject_DateTimeParser::parse($exceptionDate, $this->startDate->getTimeZone()); } } } }
echo memory_get_usage() . " BYTES<br>" . PHP_EOL; echo memory_get_peak_usage() . " BYTES<br>" . PHP_EOL; //var_dump($date_parse($rodrigo)); exit(1); try { $agora = new DateTime(); } catch (Exception $e) { echo $e->getMessage(); exit(1); } //$agora->add(new DateInterval("PT10H")); //var_dump($agora); ///DIE DIE DIE die; ///DIE DIE DIE echo "<br />Agora sao " . $agora->format($agora::ATOM) . " in " . $agora->getTimeZone()->getName(); if (isset($_GET['timezone'])) { $timezone = $_GET['timezone']; $agora->setTimeZone(new DateTimeZone($timezone)); echo "<br />Agora sao " . $agora->format($agora::RFC822) . " in " . $agora->getTimeZone()->getName(); } $inTenHours = $agora; $inTenHours->add(new DateInterval("P1YT10H10M")); echo "<br>In 1 year it will be " . $inTenHours->format($inTenHours::ATOM); $timeZones = DateTimeZone::listIdentifiers(); echo "<br><br>"; echo "<form action='index.php' method='GET'>"; echo "<select name='timezone' required>"; echo "<option required>Choose one</option>"; foreach ($timeZones as $timeZone) { echo "<option value='{$timeZone}'>{$timeZone}</option>";
/** * {@inheritdoc} */ public function collect(MvcEvent $mvcEvent) { $date = new \DateTime(); $this->environmentData = array('environment' => getenv('APPLICATION_ENV') ?: 'default', 'timezone' => $date->getTimeZone()->getName(), 'locale' => \Locale::getDefault(), 'php_extensions' => get_loaded_extensions()); }
<?php ini_set("intl.error_level", E_WARNING); var_dump(IntlTimeZone::fromDateTimeZone()); var_dump(IntlTimeZone::fromDateTimeZone(1, 2)); var_dump(IntlTimeZone::fromDateTimeZone('sdfds')); var_dump(IntlTimeZone::fromDateTimeZone(new stdclass())); $dt = new DateTime('2012-08-01 00:00:00 WEST'); var_dump(IntlTimeZone::fromDateTimeZone($dt->getTimeZone())); var_dump(intltz_from_date_time_zone());
/** * @note Drop this function when PHP support bumps to 5.6. * * @param \DateTime $datetime DateTime to convert. * * @return \DateTimeImmutable * * @since 2015-08-07 */ protected function createFromMutable(\DateTime $datetime) { $time = $datetime->format('Y-m-d H:i:s'); $tz = $datetime->getTimeZone(); $immutable = new \DateTimeImmutable($time, $tz); return $immutable; }
/** * Returns a date/time string with the specified timestamp format * * $time = Date::formatted_time('5 minutes ago'); * * @link http://www.php.net/manual/datetime.construct * * @param string $datetime_str datetime string * @param string $timestamp_format timestamp format * @param string $timezone timezone identifier * * @return string */ public static function formatted_time($datetime_str = 'now', $timestamp_format = null, $timezone = null) { $timestamp_format = $timestamp_format == null ? Date::$timestamp_format : $timestamp_format; $timezone = $timezone === null ? Date::$timezone : $timezone; $tz = new DateTimeZone($timezone ? $timezone : date_default_timezone_get()); $time = new DateTime($datetime_str, $tz); if ($time->getTimeZone()->getName() !== $tz->getName()) { $time->setTimeZone($tz); } return $time->format($timestamp_format); }
/** * Returns a date/time string with the specified timestamp format * * Example: * ~~~ * $time = Date::formatted_time('5 minutes ago'); * ~~~ * * @link http://www.php.net/manual/datetime.construct * * @param string $datetime_str Datetime string [Optional] * @param string $timestamp_format Timestamp format [Optional] * @param string $timezone Timezone identifier [Optional] * * @return string * * @uses Config::get */ public static function formatted_time($datetime_str = 'now', $timestamp_format = NULL, $timezone = NULL) { $settimezone = $timezone === NULL ? self::$timezone : $timezone; //Display Dates in site defined timezone format if (Config::get('site.timezone_override', FALSE) and $timezone === NULL) { // Default timezone from config $settimezone = Config::get('site.timezone', 'UTC'); } //convert timestamp to support datetime class if (is_numeric($datetime_str)) { $datetime_str = '@' . $datetime_str; } $timestamp_format = $timestamp_format == NULL ? self::$timestamp_format : $timestamp_format; $tz = new DateTimeZone($settimezone ? $settimezone : date_default_timezone_get()); $time = new DateTime($datetime_str, $tz); if ($time->getTimeZone()->getName() !== $tz->getName()) { $time->setTimeZone($tz); } return $time->format($timestamp_format); }
<?php ini_set("intl.error_level", E_WARNING); ini_set("intl.default_locale", "nl"); date_default_timezone_set('Europe/Lisbon'); $tz = IntlTimeZone::fromDateTimeZone(new DateTimeZone('Europe/Amsterdam')); var_dump($tz->getID(), $tz->getRawOffset()); $dt = new DateTime('2012-01-01 00:00:00 CET'); $dtz = $dt->getTimeZone(); /* this is different from new DateTimeZone('CET'), * which gives a Europe/Berlin timezone */ var_dump($dtz->getName()); $tz = IntlTimeZone::fromDateTimeZone($dtz); var_dump($tz->getID(), $tz->getRawOffset()); $dt = new DateTime('2012-01-01 00:00:00 +0340'); $dtz = $dt->getTimeZone(); /* I don't think this timezone can be generated without a DateTime object */ var_dump($dtz->getName()); $tz = IntlTimeZone::fromDateTimeZone($dtz); var_dump($tz->getID(), $tz->getRawOffset());
<?php ini_set("intl.error_level", E_WARNING); ini_set("intl.default_locale", "nl"); date_default_timezone_set('Europe/Amsterdam'); $intlcal = new IntlGregorianCalendar(); $intlcal->setTimeZone('Europe/Paris'); var_dump($intlcal->getTimeZone()->getID()); $intlcal->setTimeZone(new DateTimeZone('Europe/Madrid')); var_dump($intlcal->getTimeZone()->getID()); $pstdate = new DateTime('2012-01-01 00:00:00 PST'); $intlcal->setTimeZone($pstdate->getTimeZone()); var_dump($intlcal->getTimeZone()->getID()); $offsetdate = new DateTime('2012-01-01 00:00:00 -02:30'); $intlcal->setTimeZone($offsetdate->getTimeZone()); var_dump($intlcal->getTimeZone()->getID());
<?php $test_dates = array('2008-01-01 12:00:00 PDT', '2008-01-01 12:00:00 +02:00'); foreach ($test_dates as $test_date) { $d1 = new DateTime($test_date); $d2 = new DateTime('2008-01-01 12:00:00 UTC'); echo $d1->format(DATE_ISO8601), PHP_EOL; echo $d2->format(DATE_ISO8601), PHP_EOL; $tz = $d1->getTimeZone(); $d2->setTimeZone($tz); echo $d1->format(DATE_ISO8601), PHP_EOL; echo $d2->format(DATE_ISO8601), PHP_EOL; echo PHP_EOL; }
/** * Creates the iterator * * You should pass a VCALENDAR component, as well as the UID of the event * we're going to traverse. * * @param Component $vcal * @param string|null $uid */ public function __construct(Component $vcal, $uid = null) { if (is_null($uid)) { if ($vcal->name === 'VCALENDAR') { throw new \InvalidArgumentException('If you pass a VCALENDAR object, you must pass a uid argument as well'); } $components = array($vcal); $uid = (string) $vcal->uid; } else { $components = $vcal->select('VEVENT'); } foreach ($components as $component) { if ((string) $component->uid == $uid) { if (isset($component->{'RECURRENCE-ID'})) { $this->overriddenEvents[$component->DTSTART->getDateTime()->getTimeStamp()] = $component; $this->overriddenDates[] = $component->{'RECURRENCE-ID'}->getDateTime(); } else { $this->baseEvent = $component; } } } if (!$this->baseEvent) { throw new \InvalidArgumentException('Could not find a base event with uid: ' . $uid); } $this->startDate = clone $this->baseEvent->DTSTART->getDateTime(); $this->endDate = null; if (isset($this->baseEvent->DTEND)) { $this->endDate = clone $this->baseEvent->DTEND->getDateTime(); } else { $this->endDate = clone $this->startDate; if (isset($this->baseEvent->DURATION)) { $this->endDate->add(DateTimeParser::parse($this->baseEvent->DURATION->value)); } elseif ($this->baseEvent->DTSTART->getDateType() === Property\DateTime::DATE) { $this->endDate->modify('+1 day'); } } $this->currentDate = clone $this->startDate; $rrule = (string) $this->baseEvent->RRULE; $parts = explode(';', $rrule); // If no rrule was specified, we create a default setting if (!$rrule) { $this->frequency = 'daily'; $this->count = 1; } else { foreach ($parts as $part) { list($key, $value) = explode('=', $part, 2); switch (strtoupper($key)) { case 'FREQ': if (!in_array(strtolower($value), array('secondly', 'minutely', 'hourly', 'daily', 'weekly', 'monthly', 'yearly'))) { throw new \InvalidArgumentException('Unknown value for FREQ=' . strtoupper($value)); } $this->frequency = strtolower($value); break; case 'UNTIL': $this->until = DateTimeParser::parse($value); // In some cases events are generated with an UNTIL= // parameter before the actual start of the event. // // Not sure why this is happening. We assume that the // intention was that the event only recurs once. // // So we are modifying the parameter so our code doesn't // break. if ($this->until < $this->baseEvent->DTSTART->getDateTime()) { $this->until = $this->baseEvent->DTSTART->getDateTime(); } break; case 'COUNT': $this->count = (int) $value; break; case 'INTERVAL': $this->interval = (int) $value; if ($this->interval < 1) { throw new \InvalidArgumentException('INTERVAL in RRULE must be a positive integer!'); } break; case 'BYSECOND': $this->bySecond = explode(',', $value); break; case 'BYMINUTE': $this->byMinute = explode(',', $value); break; case 'BYHOUR': $this->byHour = explode(',', $value); break; case 'BYDAY': $this->byDay = explode(',', strtoupper($value)); break; case 'BYMONTHDAY': $this->byMonthDay = explode(',', $value); break; case 'BYYEARDAY': $this->byYearDay = explode(',', $value); break; case 'BYWEEKNO': $this->byWeekNo = explode(',', $value); break; case 'BYMONTH': $this->byMonth = explode(',', $value); break; case 'BYSETPOS': $this->bySetPos = explode(',', $value); break; case 'WKST': $this->weekStart = strtoupper($value); break; } } } // Parsing exception dates if (isset($this->baseEvent->EXDATE)) { foreach ($this->baseEvent->EXDATE as $exDate) { foreach (explode(',', (string) $exDate) as $exceptionDate) { $this->exceptionDates[] = DateTimeParser::parse($exceptionDate, $this->startDate->getTimeZone()); } } } }
/** * Returns a date/time string with the specified timestamp format * * $time = Date::formatted_time('5 minutes ago'); * * @link http://www.php.net/manual/\DateTime.construct * @param string $\DateTime_str \DateTime string * @param string $timestamp_format timestamp format * @param string $timezone timezone identifier * @return string */ public static function formattedTime($DateTime_str = 'now', $timestamp_format = null, $timezone = null) { if (!$timestamp_format) { $timestamp_format = static::$timestamp_format; } if (!$timezone) { $timezone = static::$timezone; } $tz = new \DateTimeZone($timezone); $time = new \DateTime($DateTime_str, $tz); if ($time->getTimeZone()->getName() !== $tz->getName()) { $time->setTimeZone($tz); } return $time->format($timestamp_format); }
/** * Creates the iterator * * You should pass a VCALENDAR component, as well as the UID of the event * we're going to traverse. * * @param Component $vcal * @param string|null $uid */ public function __construct(Component $vcal, $uid = null) { if (is_null($uid)) { if ($vcal instanceof Component\VCalendar) { throw new \InvalidArgumentException('If you pass a VCALENDAR object, you must pass a uid argument as well'); } $components = array($vcal); $uid = (string) $vcal->uid; } else { $components = $vcal->select('VEVENT'); } foreach ($components as $component) { if ((string) $component->uid == $uid) { if (isset($component->{'RECURRENCE-ID'})) { $this->overriddenEvents[$component->DTSTART->getDateTime()->getTimeStamp()] = $component; $this->overriddenDates[] = $component->{'RECURRENCE-ID'}->getDateTime(); } else { $this->baseEvent = $component; } } } if (!$this->baseEvent) { // No base event was found. CalDAV does allow cases where only // overridden instances are stored. // // In this barticular case, we're just going to grab the first // event and use that instead. This may not always give the // desired result. if (!count($this->overriddenEvents)) { throw new \InvalidArgumentException('Could not find an event with uid: ' . $uid); } ksort($this->overriddenEvents, SORT_NUMERIC); $this->baseEvent = array_shift($this->overriddenEvents); } $this->startDate = clone $this->baseEvent->DTSTART->getDateTime(); $this->endDate = null; if (isset($this->baseEvent->DTEND)) { $this->endDate = clone $this->baseEvent->DTEND->getDateTime(); } else { $this->endDate = clone $this->startDate; if (isset($this->baseEvent->DURATION)) { $this->endDate->add(DateTimeParser::parse((string) $this->baseEvent->DURATION)); } elseif (!$this->baseEvent->DTSTART->hasTime()) { $this->endDate->modify('+1 day'); } } $this->currentDate = clone $this->startDate; $rrule = $this->baseEvent->RRULE; // If no rrule was specified, we create a default setting if (!$rrule) { $this->frequency = 'daily'; $this->count = 1; } else { foreach ($rrule->getParts() as $key => $value) { switch ($key) { case 'FREQ': if (!in_array(strtolower($value), array('secondly', 'minutely', 'hourly', 'daily', 'weekly', 'monthly', 'yearly'))) { throw new \InvalidArgumentException('Unknown value for FREQ=' . strtoupper($value)); } $this->frequency = strtolower($value); break; case 'UNTIL': $this->until = DateTimeParser::parse($value); // In some cases events are generated with an UNTIL= // parameter before the actual start of the event. // // Not sure why this is happening. We assume that the // intention was that the event only recurs once. // // So we are modifying the parameter so our code doesn't // break. if ($this->until < $this->baseEvent->DTSTART->getDateTime()) { $this->until = $this->baseEvent->DTSTART->getDateTime(); } break; case 'COUNT': $this->count = (int) $value; break; case 'INTERVAL': $this->interval = (int) $value; if ($this->interval < 1) { throw new \InvalidArgumentException('INTERVAL in RRULE must be a positive integer!'); } break; case 'BYSECOND': $this->bySecond = (array) $value; break; case 'BYMINUTE': $this->byMinute = (array) $value; break; case 'BYHOUR': $this->byHour = (array) $value; break; case 'BYDAY': $this->byDay = (array) $value; break; case 'BYMONTHDAY': $this->byMonthDay = (array) $value; break; case 'BYYEARDAY': $this->byYearDay = (array) $value; break; case 'BYWEEKNO': $this->byWeekNo = (array) $value; break; case 'BYMONTH': $this->byMonth = (array) $value; break; case 'BYSETPOS': $this->bySetPos = (array) $value; break; case 'WKST': $this->weekStart = strtoupper($value); break; } } } // Parsing exception dates if (isset($this->baseEvent->EXDATE)) { foreach ($this->baseEvent->EXDATE as $exDate) { foreach (explode(',', (string) $exDate) as $exceptionDate) { $this->exceptionDates[] = DateTimeParser::parse($exceptionDate, $this->startDate->getTimeZone()); } } } }
public function __toString() { return (string) parent::format(self::$Format) . ' ' . parent::getTimeZone()->getName(); }
/** * Create a Carbon instance from a DateTime one * * @param DateTime $dt * * @return static */ public static function instance(DateTime $dt) { return new static($dt->format('Y-m-d H:i:s.u'), $dt->getTimeZone()); }
<?php /* Prototype : public DateTimeZone DateTime::getTimezone ( void ) * Description: Return time zone relative to given DateTime * Source code: ext/date/php_date.c * Alias to functions: date_timezone_get */ echo "*** Testing DateTime::getTimezone() : basic functionality ***\n"; date_default_timezone_set("Europe/London"); $object = new DateTime("2009-01-30 17:57:32"); var_dump($object->getTimeZone()->getName()); date_default_timezone_set("America/New_York"); $object = new DateTime("2009-01-30 17:57:32"); var_dump($object->getTimeZone()->getName()); $la_time = new DateTimeZone("America/Los_Angeles"); $object->setTimeZone($la_time); var_dump($object->getTimeZone()->getName()); ?> ===DONE===