2010-01-14 23:41:37 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2010-10-06 16:02:20 +02:00
|
|
|
* This file is part of the "dibi" - smart database abstraction layer.
|
2012-01-02 20:24:16 +01:00
|
|
|
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
2010-01-14 23:41:37 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DateTime with serialization and timestamp support for PHP 5.2.
|
|
|
|
*
|
2010-09-14 18:40:41 +02:00
|
|
|
* @author David Grudl
|
2012-01-03 04:50:11 +01:00
|
|
|
* @package dibi
|
2010-01-14 23:41:37 +01:00
|
|
|
*/
|
2010-10-06 16:02:20 +02:00
|
|
|
class DibiDateTime extends DateTime
|
2010-01-14 23:41:37 +01:00
|
|
|
{
|
2010-01-23 04:45:59 +01:00
|
|
|
|
2011-01-24 20:47:35 +01:00
|
|
|
public function __construct($time = 'now', DateTimeZone $timezone = NULL)
|
|
|
|
{
|
|
|
|
if (is_numeric($time)) {
|
2013-12-19 04:21:45 +01:00
|
|
|
parent::__construct('@' . $time);
|
|
|
|
$this->setTimeZone($timezone ? $timezone : new DateTimeZone(date_default_timezone_get()));
|
2013-12-12 15:57:34 +01:00
|
|
|
} elseif ($timezone === NULL) {
|
2011-01-24 20:47:35 +01:00
|
|
|
parent::__construct($time);
|
|
|
|
} else {
|
|
|
|
parent::__construct($time, $timezone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-12 14:59:56 +01:00
|
|
|
public function modifyClone($modify = '')
|
|
|
|
{
|
|
|
|
$dolly = clone($this);
|
|
|
|
return $modify ? $dolly->modify($modify) : $dolly;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function modify($modify)
|
|
|
|
{
|
|
|
|
parent::modify($modify);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-12 15:57:34 +01:00
|
|
|
public function setTimestamp($timestamp)
|
|
|
|
{
|
|
|
|
$zone = PHP_VERSION_ID === 50206 ? new \DateTimeZone($this->getTimezone()->getName()) : $this->getTimezone();
|
|
|
|
$this->__construct('@' . $timestamp);
|
|
|
|
$this->setTimeZone($zone);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getTimestamp()
|
|
|
|
{
|
|
|
|
$ts = $this->format('U');
|
|
|
|
return is_float($tmp = $ts * 1) ? $ts : $tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return $this->format('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-14 23:41:37 +01:00
|
|
|
public function __sleep()
|
|
|
|
{
|
2013-09-30 01:09:14 +02:00
|
|
|
$zone = $this->getTimezone()->getName();
|
|
|
|
if ($zone[0] === '+') {
|
|
|
|
$this->fix = array($this->format('Y-m-d H:i:sP'));
|
|
|
|
} else {
|
|
|
|
$this->fix = array($this->format('Y-m-d H:i:s'), $zone);
|
|
|
|
}
|
2010-01-14 23:41:37 +01:00
|
|
|
return array('fix');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function __wakeup()
|
|
|
|
{
|
2013-09-30 01:09:14 +02:00
|
|
|
if (isset($this->fix[1])) {
|
|
|
|
$this->__construct($this->fix[0], new DateTimeZone($this->fix[1]));
|
|
|
|
} else {
|
|
|
|
$this->__construct($this->fix[0]);
|
|
|
|
}
|
2010-01-14 23:41:37 +01:00
|
|
|
unset($this->fix);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|