note 76986 added to function.date-modify

From: Date: Wed, 08 Aug 2007 23:17:22 +0000
Subject: note 76986 added to function.date-modify
Groups: php.notes 
Request: Send a blank email to php-notes+get-129362@lists.php.net to get a copy of this message
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')); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 74.95.9.105 ---- Manual Page -- http://www.php.net/manual/en/function.date-modify.php Edit -- https://master.php.net/note/edit/76986 Del: integrated -- https://master.php.net/note/delete/76986/integrated Del: useless -- https://master.php.net/note/delete/76986/useless Del: bad code -- https://master.php.net/note/delete/76986/bad+code Del: spam -- https://master.php.net/note/delete/76986/spam Del: non-english -- https://master.php.net/note/delete/76986/non-english Del: in docs -- https://master.php.net/note/delete/76986/in+docs Del: other reasons-- https://master.php.net/note/delete/76986 Reject -- https://master.php.net/note/reject/76986 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129362) next »