Hi there,
if you use previous code on x64 architecture and got a php version prior to 5.2.7 (debian lenny for instance), beware of bug #45038 (http://bugs.php.net/bug.php?id=45038)
a solution can be to use following code:
<?php
function setTimestamp($timestamp) {
$thisz_original = $this -> getTimezone()->getName();
$thisz_utc = new DateTimeZone('UTC');
$this -> setTimezone($thisz_utc);
$year = gmdate("Y",$timestamp);
$month = gmdate("n",$timestamp);
$day = gmdate("j",$timestamp);
$hour = gmdate("G",$timestamp);
$minute = gmdate("i",$timestamp);
$second = gmdate("s",$timestamp);
$this -> setDate($year,$month,$day);
$this -> setTime($hour,$minute,$second);
$this -> setTimezone(new DateTimeZone($thisz_original));
}
?>
DateTime::setTimestamp
(PHP 5 >= 5.3.0)
DateTime::setTimestamp — Sets the date and time based on an Unix timestamp
Beschreibung
Sets the date and time based on an Unix timestamp.
Parameter-Liste
- unixtimestamp
-
Unix timestamp representing the date.
Rückgabewerte
Returns the modified DateTime.
DateTime::setTimestamp
julien_muetton at carpe-hora dot com
28-Nov-2009 12:48
28-Nov-2009 12:48
edwin dot h at clear dot net dot nz
01-Oct-2009 08:55
01-Oct-2009 08:55
If you are using PHP < 5.3.0 you can use this function instead:
<?php
function DateTime_setTimestamp(&$dt,$timestamp) {
$dtz_original = $dt -> getTimezone();
$dtz_utc = new DateTimeZone("UTC");
$dt -> setTimezone($dtz_utc);
$year = gmdate("Y",$timestamp);
$month = gmdate("n",$timestamp);
$day = gmdate("j",$timestamp);
$hour = gmdate("G",$timestamp);
$minute = gmdate("i",$timestamp);
$second = gmdate("s",$timestamp);
$dt -> setDate($year,$month,$day);
$dt -> setTime($hour,$minute,$second);
$dt -> setTimezone($dtz_original);
}
?>
