theobros / settimezone
Eloquent模型中的时间戳转换,Laravel-11*中的用户时区时间转换函数
v2.0.1
2024-09-23 16:27 UTC
This package is auto-updated.
Last update: 2024-09-23 16:48:23 UTC
README
Eloquent模型中的时间戳转换,Laravel-11.*中的时区时间转换函数
安装
composer require theobros/settimezone
使用
步骤1
php artisan vendor:publish --provider="Theobros\SetTimezone\Providers\SetTimezoneServiceProvider"
步骤2
检查app.php中默认时区是否设置为UTC或其他时区
步骤3
确保所有HTTP请求中发送了timezone参数,例如值为"Asia/kolkata"或使用app.php中的默认时区来转换时间戳
步骤4
在所需的Eloquent模型中导入SetTimeZone
<?php
namespace App\Models;
use Theobros\SetTimezone\Traits\SetTimeZone;
class User extends Authenticatable
{
use SetTimeZone;
}
read_at, created_at, updated_at, updated_at等时间戳将自动格式化为默认日期时间格式"Y-m-d h:i A"。您可以在config/settimezone.php中更改默认日期时间格式
附加功能
函数调用
use SetTimeZone;
public function getCreatedAtAttribute($value)
{
return $this->convertToUser($value, 'Y-m-d h:i: A');
}
public function getCreatedAtAttribute($value)
{
return $this->convertToUTC($value, 'Y-m-d h:i: A');
}
安全
如果您发现任何安全问题,请通过电子邮件theobros05@gmail.com联系,而不是使用问题跟踪器。