Skip to content

Commit 26586dc

Browse files
committed
optional date formats
1 parent 90b6f4b commit 26586dc

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/Model.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ private static function _setDataType($field, &$value) {
218218
settype($value, $type);
219219
}
220220
}
221+
222+
return $type;
221223
}
222224

223225
public static function getDataType($field) {
@@ -454,16 +456,22 @@ public function json($flags = JSON_PRETTY_PRINT) {
454456
return json_encode($this->toArray(), $flags);
455457
}
456458

457-
public function toArray($get_fields = null) {
459+
public function toArray($fields = null, $dates_format = null) {
458460
$model = self::_getModelInfo();
459461
if (!$model) return false;
460462

461463
$result = [];
462464
$properties = get_object_vars($this);
463465

464466
foreach ($properties as $field => $value) {
465-
if (!$get_fields || ($get_fields && in_array($field, $get_fields))) {
466-
self::_setDataType($field, $value);
467+
if (!$fields || ($fields && in_array($field, $fields))) {
468+
$type = self::_setDataType($field, $value);
469+
470+
// format dates if specified
471+
if ($dates_format && ($type === DB::TYPE_DATETIME || in_array($field, ['created_at', 'updated_at', 'deleted_at']))) {
472+
$value = date($dates_format, strtotime($value));
473+
}
474+
467475
$result[$field] = $value;
468476
}
469477
}

0 commit comments

Comments
 (0)