|
date_modify
Alters the timestamp
(PHP 5 >= 5.1.0)
Example 440. A date_modify() example<?php The above example will output: 2006-12-13 Code Examples / Notes » date_modifymike_d_olson
I had problems with setting an existing DateTime object to an exact Unix timestamp using modify("@$timestamp"), which seems to always be relative. So I wrote this function, which does the trick: <?php function set_time(DateTime $dt, $timestamp) { $tzo = new DateTimeZone($dt->getTimezone()->getName()); $new_dt = new DateTime("@$timestamp", new DateTimeZone('UTC')); $new_dt->setTimezone($tzo); $dt->setDate($new_dt->format('Y'), $new_dt->format('m'), $new_dt->format('d')); $dt->setTime($new_dt->format('H'), $new_dt->format('i'), $new_dt->format('s')); } ?> someone
I decided to enhance the DateTime object by taking advantage of method chaining. <?php class DateTimeChain extends DateTime { public function modify ($modify) { parent::modify($modify); return $this; } public function setDate ($year, $month, $day) { parent::setDate($year, $month, $day); return $this; } public function setISODate ($year, $week, $day = null) { parent:: setISODate($year, $week, $day); return $this; } public function setTime ($hour, $minute, $second = null) { parent::setTime($hour, $minute, $second); return $this; } public function setTimezone ($timezone) { parent::setTimezone($timezone); return $this; } } $t = new DateTimeZone('America/Los_Angeles'); $d = new DateTimeChain(); var_dump($d->setTimezone($t)->modify('5years')->format(DATE_RFC822)); ?> |
Change Languagecheckdate date_create date_date_set date_default_timezone_get date_default_timezone_set date_format date_isodate_set date_modify date_offset_get date_parse date_sun_info date_sunrise date_sunset date_time_set date_timezone_get date_timezone_set date getdate gettimeofday gmdate gmmktime gmstrftime idate localtime microtime mktime strftime strptime strtotime time timezone_abbreviations_list timezone_identifiers_list timezone_name_from_abbr timezone_name_get timezone_offset_get timezone_open timezone_transitions_get |