Laravel Carbon日期两个日期之间的差异

时间:2018-02-11 15:12:21

标签: php laravel php-carbon

我的数据库中有一个字段,它有一个使用时刻js的时间戳。结果是这样的。

["2018-02-11 11:30:17","2018-02-11 11:20:17","2018-02-11 11:10:17"]

但是当我从db返回created_at colum时,数组就像这样给出:

[{"date":"2018-02-11 11:40:17.000000","timezone_type":3,"timezone":"Asia\/Karachi"},{"date":"2018-02-11 11:40:31.000000","timezone_type":3,"timezone":"Asia\/Karachi"},{"date":"2018-02-11 11:40:40.000000","timezone_type":3,"timezone":"Asia\/Karachi"}]

那么我如何以碳可以理解的格式获取两个列日期?我想要" starttime"要与" created_at"进行比较的列。这可以实现吗?到目前为止,这是我的代码:

$cleanStart = Clean::pluck('starttime')->toArray();
$cleanFinish = Clean::pluck('created_at')->toArray();

$from = Carbon::parse($cleanStart);
$to = Carbon::parse($cleanFinish);
$diff_in_hours = $to->diffInHours($from);

return $diff_in_hours; 

但它给了我一个错误:

Type error: DateTime::__construct() expects parameter 1 to be string, array given

另外,我如何将阵列赋予碳。

所以最后这是我尝试过的事情:

$cleanCollection = Clean::get(['starttime','created_at']);
        foreach($cleanCollection as $cleanObj){
            $startTime = Carbon::parse($cleanObj->starttime);
            $diff = $cleanObj->created_at->diffInseconds($startTime);
        }


       echo $diff;

但是当我刷新页面时,值会以秒为单位变化。如果添加了另一条记录,它会再次累加。

2 个答案:

答案 0 :(得分:2)

Pluck将为您提供结果集中所有开始时间的数组,这就是您将数组传递到parse的原因。您实际上已经获得了所有的开始时间和所有创建的ats,然后尝试有效地比较所有开始时间。

您需要获得一个结果,

$clean = Clean::first();
$from = Carbon::parse($clean->starttime);
$to = Carbon::parse($clean->created_at);
$diff_in_hours = $to->diffInHours($from);

或者,如果你想要每一行,你必须迭代它们并做同样的事情

Clean::all()->each(function ($clean) {
    $from = Carbon::parse($clean->starttime);
    $to = Carbon::parse($clean->created_at);
    $diff_in_hours = $to->diffInHours($from); // for this row
});

您可以做的另一件事是在Clean模型上放置一个访问器来帮助您解决这个问题

public function getDiffInHoursAttribute()
{
    $from = Carbon::parse($this->starttime);
    $to = Carbon::parse($this->created_at);
    return $to->diffInHours($from);
}

然后

echo Clean::first()->diffInHours;

或者

foreach(Clean::all() as $clean) {
    echo $clean->diffInHours;
}

此外,如果您将以下内容添加到模型中,Eloquent会自动将字符串解析为Carbon对象,以便您可以在代码中跳过对Carbon :: parse()的需求

/**
 * The attributes that should be mutated to dates.
 *
 * @var array
 */
protected $dates = [
    'starttime'
];

答案 1 :(得分:1)

尝试将protected $dates添加到Clean模型中,如下所示:

/**
 * The attributes that should be mutated to dates.
 *
 * @var array
 */
protected $dates = [
    'created_at',
    'updated_at'
];

正如您可以从代码中的注释中读取的那样,将所有应转换为日期的列放入其中,这将有助于您更轻松地实现日期操作。

编辑1:

$start = new Carbon($cleanStart['date'], $cleanStart['timezone']);
$finish = new Carbon($cleanFinish['date'], $cleanFinish['timezone']);

然后你可以这样比较:

var_dump($start->eq($finish)); //Is start date same as finish date
var_dump($start->ne($finish)); //Is start date not same as finish date
var_dump($start->gt($finish)); //Is start date greater than finish date
var_dump($start->gte($finish)); //Is start date greater than or equal to finish date
var_dump($start->lt($finish)); //Is start date less than finish date
var_dump($start->lte($finish)); //Is start date less than or equal to finish date

编辑2:

为了使下面的代码有效,您必须在$start

中初始化$finishEDIT 1日期
$ago = $start->diffForHumans($finish, true); //2 days OR 3 weeks OR 1 hour