From a8e83ce93d1736391f7f17ce627cc499a1ae130c Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 26 Jan 2010 01:38:47 +0100 Subject: [PATCH] DibiRow: added asTimestamp() & asDateTime(), therefore asDate() is deprecated --- dibi/libs/DibiRow.php | 49 ++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/dibi/libs/DibiRow.php b/dibi/libs/DibiRow.php index 854c624b..1940f7f2 100644 --- a/dibi/libs/DibiRow.php +++ b/dibi/libs/DibiRow.php @@ -32,30 +32,31 @@ class DibiRow extends ArrayObject /** - * Converts value to date-time format. + * Converts value to DateTime object. * @param string key - * @param string format (TRUE means DateTime object) - * @return mixed + * @return DateTime */ - public function asDate($key, $format = NULL) + public function asDateTime($key) { $time = $this[$key]; - if ((int) $time === 0) { // '', NULL, FALSE, '0000-00-00', ... - return NULL; + return (int) $time === 0 // '', NULL, FALSE, '0000-00-00', ... + ? NULL + : new DateTime53(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time); + } - } elseif ($format === NULL) { // return timestamp (default) - return is_numeric($time) ? (int) $time : strtotime($time); - } elseif ($format === TRUE) { // return DateTime object - return new DateTime53(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time); - } elseif (is_numeric($time)) { // single timestamp - return date($format, $time); - - } else { - $time = new DateTime53($time); - return $time->format($format); - } + /** + * Converts value to UNIX timestamp. + * @param string key + * @return int + */ + public function asTimestamp($key) + { + $time = $this[$key]; + return (int) $time === 0 // '', NULL, FALSE, '0000-00-00', ... + ? NULL + : (is_numeric($time) ? (int) $time : strtotime($time)); } @@ -87,4 +88,18 @@ class DibiRow extends ArrayObject $this->setFlags(2); } + + + /** @deprecated */ + public function asDate($key, $format = NULL) + { + if ($format === NULL) { + return $this->asTimestamp($key); + } elseif ($format === TRUE) { + return $this->asDateTime($key); + } else { + return $this->asDateTime($key)->format($format); + } + } + }