在 Laravel 中使用时区将默认数据库 DateFormat 设置为 ISO 8601

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25023790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:52:45  来源:igfitidea点击:

Set default database DateFormat to ISO 8601 with timezones in Laravel

phpmysqllaraveleloquentiso8601

提问by custi

I think the title says it: I want to use ISO 8601 with timezones as default DateTime-Format in Laravels Eloquent. I got this

我认为标题说明了这一点:我想在 Laravel Eloquent 中使用带有时区的 ISO 8601 作为默认日期时间格式。我懂了

class mEloquent extends Eloquent {

   protected function getDateFormat()
   {
       return 'YYYY-MM-DDThh:mm:ss.sTZD';
   }

}

All my models will extend mEloquent. But how should I build the tables? Just

我所有的模型都将扩展 mEloquent。但是我应该如何构建表格?只是

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateUsersTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('firstName');
        $table->string('lastName');
        $table->string('email')->unique();
        $table->string('password');
        $table->timestamps();
    });
} ......

as normal? How can I compare this dateformat? Or shoud I just use the default one and save the timezone separately?

像平常一样?我如何比较这个日期格式?或者我应该只使用默认值并单独保存时区?

采纳答案by Christos Lytras

It's really easy if you follow the documents.

如果您按照文档进行操作,这真的很容易。

PHP datefunction supports ISO 8601since PHP5 and we can get it by passing the 'c'format character.

PHPdate函数从 PHP5 开始支持ISO 8601,我们可以通过传递'c'格式字符来获取它。

http://php.net/manual/en/function.date.php

http://php.net/manual/en/function.date.php

Laravel model converts date attributes to Carbonobjects. Carbon extends DateTimewhich has the formatfunction that supports all of the dateformat characters.

Laravel 模型将日期属性转换为Carbon对象。Carbon extendsDateTime具有format支持所有date格式字符的功能。

You can easily create an accessor (or even a new custom attribute) to change a date attribute. Then use the Carbon formatmethod to change it to ISO 8601format.

您可以轻松创建访问器(甚至是新的自定义属性)来更改日期属性。然后使用Carbonformat方法将其更改为ISO 8601格式。

So in a laravel model, we can do something like this:

所以在 Laravel 模型中,我们可以这样做:

public function getPublishedAt8601Attribute()
{
    return $this->published_at->format('c');
}

and then we can access the attribute like this:

然后我们可以像这样访问属性:

// Prints something like: 2016-10-13T21:48:00+03:00
echo $post->published_at_8601;

回答by Christos Lytras

As mentioned in the docs, add this to your base class 'mEloquent'

如文档中所述,将其添加到您的基类“mEloquent”中

/**
 * The storage format of the model's date columns.
 *
 * @var string
 */
protected $dateFormat = 'c';

You can also check out date serializations: https://laravel.com/docs/5.6/eloquent-serialization#date-serialization

您还可以查看日期序列化:https: //laravel.com/docs/5.6/eloquent-serialization#date-serialization

回答by Kvlknctk

$date = Carbon::now();
echo $date->toW3cString();

$date = $this->repository->find($id);
echo $date->created_at->toW3cString();

Output

输出

2018-04-14T18:35:58+00:00

2018-04-14T18:35:58+00:00

回答by Shamsheer

Best Solution:

最佳解决方案:

$order_date = $order->created_at;
$date_changed=$order_date->toIso8601String();

回答by JR Lawhorne

In your Eloquentmodel, set the $dateFormatvariable to DateTime::ISO8601

在您的Eloquent模型中,将$dateFormat变量设置为DateTime::ISO8601

    protected $dateFormat = DateTime::ISO8601;

Some others mentioned using the 'c'format but I found that doesn't work because Laravel uses the DateTime::createFromFormatfunction when converting the DateTime which does not support the 'c'option.

其他一些人提到使用该'c'格式,但我发现这不起作用,因为 LaravelDateTime::createFromFormat在转换不支持该'c'选项的 DateTime 时使用了该函数。