1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-10 08:04:32 +02:00

dibi internally uses DateTime object in PHP 5.2

This commit is contained in:
David Grudl
2009-08-20 23:42:50 +02:00
parent 3777bacc02
commit fa6d771813
14 changed files with 76 additions and 44 deletions

View File

@@ -500,7 +500,7 @@ class DibiResult extends DibiObject implements IDataSource
final public function convert($value, $type, $format = NULL)
{
if ($value === NULL || $value === FALSE) {
return $value;
return NULL;
}
switch ($type) {
@@ -518,8 +518,25 @@ class DibiResult extends DibiObject implements IDataSource
case dibi::DATE:
case dibi::DATETIME:
$value = is_numeric($value) ? (int) $value : strtotime($value);
return $format === NULL ? $value : date($format, $value);
if ($value == NULL) { // intentionally ==
return NULL;
} elseif ($format === NULL) { // return timestamp (default)
return is_numeric($value) ? (int) $value : strtotime($value);
} elseif ($format === TRUE) { // return DateTime object
return new DateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
} elseif (is_numeric($value)) { // single timestamp
return date($format, $value);
} elseif (class_exists('DateTime', FALSE)) { // since PHP 5.2
$value = new DateTime($value);
return $value ? $value->format($format) : NULL;
} else {
return date($format, strtotime($value));
}
case dibi::BOOL:
return ((bool) $value) && $value !== 'f' && $value !== 'F';

View File

@@ -42,18 +42,30 @@ class DibiRow extends ArrayObject
/**
* Converts value to date-time format.
* @param string key
* @param string format
* @param string format (TRUE means DateTime object)
* @return mixed
*/
public function asDate($key, $format = NULL)
{
$value = $this[$key];
if ($value === NULL || $value === FALSE) {
return $value;
$time = $this[$key];
if ($time == NULL) { // intentionally ==
return NULL;
} elseif ($format === NULL) { // return timestamp (default)
return is_numeric($time) ? (int) $time : strtotime($time);
} elseif ($format === TRUE) { // return DateTime object
return new DateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
} elseif (is_numeric($time)) { // single timestamp
return date($format, $time);
} elseif (class_exists('DateTime', FALSE)) { // since PHP 5.2
$time = new DateTime($time);
return $time ? $time->format($format) : NULL;
} else {
$value = is_numeric($value) ? (int) $value : strtotime($value);
return $format === NULL ? $value : date($format, $value);
return date($format, strtotime($time));
}
}

View File

@@ -372,7 +372,13 @@ final class DibiTranslator extends DibiObject
if ($value === NULL) {
return 'NULL';
} else {
return $this->driver->escape(is_numeric($value) ? (int) $value : strtotime($value), $modifier);
if (is_numeric($value)) {
$value = (int) $value; // timestamp
} elseif (is_string($value)) {
$value = class_exists('DateTime', FALSE) ? new DateTime($value) : strtotime($value);
}
return $this->driver->escape($value, $modifier);
}
case 'by':