Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
}
}
}
?>
@SeanJA
Copy link
Author

SeanJA commented Mar 31, 2010

That should be good, I don't think I need to make any more modifications... I hope?

@mundanelunacy
Copy link

mundanelunacy commented Nov 10, 2013

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