downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DateTime::setTimezone> <DateTime::setTime
Last updated: Fri, 11 Dec 2009

view this page in

DateTime::setTimestamp

(PHP 5 >= 5.3.0)

DateTime::setTimestampSets the date and time based on an Unix timestamp

Description

public DateTime DateTime::setTimestamp ( int $unixtimestamp )

Sets the date and time based on an Unix timestamp.

Parameters

unixtimestamp

Unix timestamp representing the date.

Return Values

Returns the modified DateTime.

See Also



add a note add a note User Contributed Notes
DateTime::setTimestamp
julien_muetton at carpe-hora dot com
28-Nov-2009 12:48
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));
  }

?>
edwin dot h at clear dot net dot nz
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);
}
?>

DateTime::setTimezone> <DateTime::setTime
Last updated: Fri, 11 Dec 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites