feat: 模型基类增加时间戳格式的通用处理方法

This commit is contained in:
augushong
2025-03-28 11:02:45 +08:00
parent e97d38d9e4
commit 4935f06200

View File

@@ -37,4 +37,33 @@ class TimeModelBase extends BaseModel
protected $deleteTime = 'delete_time';
protected $defaultSoftDelete = 0;
public static function timeAttrSet($value)
{
if (empty($value)) {
return 0;
}
if (is_numeric($value)) {
return $value;
}
$time = strtotime($value);
if ($time === false) {
return 0;
}
return $time;
}
public static function timeAttrGet($value, $format = 'Y-m-d H:i:s')
{
if (empty($value)) {
return '';
}
if (is_numeric($value)) {
return date($format, $value);
}
return $value;
}
}