mirror of
https://github.com/dg/dibi.git
synced 2025-08-06 06:07:39 +02:00
DibiRow: added asTimestamp() & asDateTime(), therefore asDate() is deprecated
This commit is contained in:
@@ -32,30 +32,31 @@ class DibiRow extends ArrayObject
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts value to date-time format.
|
* Converts value to DateTime object.
|
||||||
* @param string key
|
* @param string key
|
||||||
* @param string format (TRUE means DateTime object)
|
* @return DateTime
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
public function asDate($key, $format = NULL)
|
public function asDateTime($key)
|
||||||
{
|
{
|
||||||
$time = $this[$key];
|
$time = $this[$key];
|
||||||
if ((int) $time === 0) { // '', NULL, FALSE, '0000-00-00', ...
|
return (int) $time === 0 // '', NULL, FALSE, '0000-00-00', ...
|
||||||
return NULL;
|
? 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);
|
* Converts value to UNIX timestamp.
|
||||||
|
* @param string key
|
||||||
} else {
|
* @return int
|
||||||
$time = new DateTime53($time);
|
*/
|
||||||
return $time->format($format);
|
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);
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user