Skip to content

Instantly share code, notes, and snippets.

@SeanJA
Created March 30, 2010 16:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanJA/349273 to your computer and use it in GitHub Desktop.
Save SeanJA/349273 to your computer and use it in GitHub Desktop.
A fix for missing functionality in php 5.2's DateTime class
<?php
/**
* Provides backwards support for php 5.2's lack of setTimestamp and getTimestamp
*/
class DateTime_52 extends DateTime{
/**
* Set the time of the datetime object by a unix timestamp
* @param int $unixtimestamp
* @return DateTime_52
*/
public function setTimestamp($unixtimestamp){
if(!is_numeric($unixtimestamp) && !is_null($unixtimestamp)){
trigger_error('DateTime::setTimestamp() expects parameter 1 to be long, '.gettype($unixtimestamp).' given', E_USER_WARNING);
} else {
$this->setDate(date('Y', $unixtimestamp), date('n', $unixtimestamp), date('d', $unixtimestamp));
$this->setTime(date('G', $unixtimestamp), date('i', $unixtimestamp), date('s', $unixtimestamp));
}
return $this;
}
/**
* Get the time of the datetime object as a unix timestamp
* @return int a unix timestamp representing the time in the datetime object
*/
public function getTimestamp(){
return $this->format('U');
}
}
if(!function_exists('date_timestamp_get')){
/**
* Get the timestamp of a DateTime object
* @param DateTime $object
* @see DateTime_52::getTimestamp
*/
function date_timestamp_get(DateTime &$object){
return $object->getTimestamp();
}
}
if(!function_exists('date_timestamp_set')){
/**
* Set the timestamp of a DateTime object
* @param DateTime $object
* @param int $unixtimestamp
* @return DateTime_52
* @see DateTime_52::setTimestamp
*/
function date_timestamp_set(DateTime &$object, $unixtimestamp){
return $object->setTimestamp($unixtimestamp);
}
}
<?php
require_once 'PHPUnit/Framework.php';
require_once 'datetime52.class.php';
/**
* Test class for DateTime_52.
* Generated by PHPUnit on 2010-03-30 at 16:33:49.
*/
class DateTime_52Test extends PHPUnit_Framework_TestCase {
/**
* @var DateTime_52
*/
protected $dt52;
/**
* @var DateTime
*/
protected $dt;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
$this->dt52 = new DateTime_52;
$this->dt = new DateTime;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
public function testTimestamp() {
$timestamps = array(
0,
Null,
'12345',
'-10',
);
foreach($timestamps as $value){
$result52 = $this->dt52->setTimestamp($value);
$result = $this->dt->setTimestamp($value);
$this->assertEquals($result->getTimestamp(), $result52->getTimestamp());
}
$invalidTimestamps = array(
array(),
(object)array(),
'asdf',
PHP_EOL,
1.2,
PHP_INT_MAX,
);
foreach($invalidTimestamps as $value){
try{
$result52 = $this->dt52->setTimestamp($value);
$this->fail('datetime did not throw an exception.');
}catch(Exception $e){
$message52 = $e->getMessage();
}
try{
$result = $this->dt->setTimestamp($value);
$this->fail('datetime did not throw an exception.');
}catch(Exception $e){
$message = $e->getMessage();
}
$this->assertEquals($message, $message52);
}
}
}
?>
@mundanelunacy
Copy link

Thanks for the code. I added the following to the setTime function to support different timezones.

public function setTimestamp($unixtimestamp){
    if(!is_numeric($unixtimestamp) && !is_null($unixtimestamp)){
        trigger_error('DateTime::setTimestamp() expects parameter 1 to be long, '.gettype($unixtimestamp).' given', E_USER_WARNING);
    } else {

        $sys_tz = date_default_timezone_get();
        date_default_timezone_set($this->getTimezone()->getName());

        $this->setDate(date('Y', $unixtimestamp), date('n', $unixtimestamp), date('d', $unixtimestamp));
        $this->setTime(date('G', $unixtimestamp), date('i', $unixtimestamp), date('s', $unixtimestamp));

        date_default_timezone_set($sys_tz);

    }
    return $this;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment