1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-24 02:43:09 +01:00
php-dibi/dibi/libs/DibiDateTime.php

90 lines
1.8 KiB
PHP
Raw Normal View History

<?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)
*/
/**
* 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-10-06 16:02:20 +02:00
class DibiDateTime extends DateTime
{
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()));
} elseif ($timezone === NULL) {
parent::__construct($time);
} else {
parent::__construct($time, $timezone);
}
}
public function modifyClone($modify = '')
{
$dolly = clone($this);
return $modify ? $dolly->modify($modify) : $dolly;
}
public function modify($modify)
{
parent::modify($modify);
return $this;
}
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');
}
public function __sleep()
{
$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);
}
return array('fix');
}
public function __wakeup()
{
if (isset($this->fix[1])) {
$this->__construct($this->fix[0], new DateTimeZone($this->fix[1]));
} else {
$this->__construct($this->fix[0]);
}
unset($this->fix);
}
}