mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 13:47:33 +02:00
added DateTime53, fix for crappy DateTime in PHP 5.2
This commit is contained in:
54
dibi/Nette/DateTime53.php
Normal file
54
dibi/Nette/DateTime53.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Nette Framework
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2004, 2010 David Grudl
|
||||||
|
* @license http://nettephp.com/license Nette license
|
||||||
|
* @link http://nettephp.com
|
||||||
|
* @category Nette
|
||||||
|
* @package Nette
|
||||||
|
*/
|
||||||
|
|
||||||
|
// no namespace
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DateTime with serialization and timestamp support for PHP 5.2.
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2004, 2010 David Grudl
|
||||||
|
* @package Nette
|
||||||
|
*/
|
||||||
|
class DateTime53 extends DateTime
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __sleep()
|
||||||
|
{
|
||||||
|
$this->fix = array($this->format('Y-m-d H:i:s'), $this->getTimezone()->getName());
|
||||||
|
return array('fix');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public function __wakeup()
|
||||||
|
{
|
||||||
|
$this->__construct($this->fix[0], new DateTimeZone($this->fix[1]));
|
||||||
|
unset($this->fix);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public function getTimestamp()
|
||||||
|
{
|
||||||
|
return (int) $this->format('U');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public function setTimestamp($timestamp)
|
||||||
|
{
|
||||||
|
return $this->__construct(date('Y-m-d H:i:s', $timestamp), new DateTimeZone($this->getTimezone()->getName())); // getTimeZone() crashes in PHP 5.2.6
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -60,12 +60,16 @@ if (!interface_exists(/*Nette\*/'IDebuggable', FALSE)) {
|
|||||||
require_once dirname(__FILE__) . '/Nette/IDebuggable.php';
|
require_once dirname(__FILE__) . '/Nette/IDebuggable.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!class_exists('DateTime53', FALSE)) {
|
||||||
|
require_once dirname(__FILE__) . '/Nette/DateTime53.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Back-compatibility
|
* Back-compatibility
|
||||||
*/
|
*/
|
||||||
class DibiVariable extends DateTime
|
class DibiVariable extends DateTime53
|
||||||
{
|
{
|
||||||
function __construct($val)
|
function __construct($val)
|
||||||
{
|
{
|
||||||
@@ -587,7 +591,7 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
public static function datetime($time = NULL)
|
public static function datetime($time = NULL)
|
||||||
{
|
{
|
||||||
return new DateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
|
return new DateTime53(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -597,7 +601,7 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
public static function date($date = NULL)
|
public static function date($date = NULL)
|
||||||
{
|
{
|
||||||
return new DateTime(is_numeric($date) ? date('Y-m-d', $date) : $date);
|
return new DateTime53(is_numeric($date) ? date('Y-m-d', $date) : $date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -623,13 +623,13 @@ class DibiResult extends DibiObject implements IDataSource
|
|||||||
return is_numeric($value) ? (int) $value : strtotime($value);
|
return is_numeric($value) ? (int) $value : strtotime($value);
|
||||||
|
|
||||||
} elseif ($format === TRUE) { // return DateTime object
|
} elseif ($format === TRUE) { // return DateTime object
|
||||||
return new DateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
|
return new DateTime53(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
|
||||||
|
|
||||||
} elseif (is_numeric($value)) { // single timestamp
|
} elseif (is_numeric($value)) { // single timestamp
|
||||||
return date($format, $value);
|
return date($format, $value);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$value = new DateTime($value);
|
$value = new DateTime53($value);
|
||||||
return $value->format($format);
|
return $value->format($format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -47,13 +47,13 @@ class DibiRow extends ArrayObject
|
|||||||
return is_numeric($time) ? (int) $time : strtotime($time);
|
return is_numeric($time) ? (int) $time : strtotime($time);
|
||||||
|
|
||||||
} elseif ($format === TRUE) { // return DateTime object
|
} elseif ($format === TRUE) { // return DateTime object
|
||||||
return new DateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
|
return new DateTime53(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
|
||||||
|
|
||||||
} elseif (is_numeric($time)) { // single timestamp
|
} elseif (is_numeric($time)) { // single timestamp
|
||||||
return date($format, $time);
|
return date($format, $time);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$time = new DateTime($time);
|
$time = new DateTime53($time);
|
||||||
return $time->format($format);
|
return $time->format($format);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user