简体   繁体   English

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

[英]Laravel Carbon Date Difference between two dates

I have a field in my DB which has a time stamp using moment js. 我的数据库中有一个字段,它有一个使用时刻js的时间戳。 The result is like so. 结果是这样的。

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

But when i return created_at colum from db the array is given like so: 但是当我从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"}]

So how can i take two column dates in a format where carbon can understand? 那么我怎样才能以碳可以理解的格式获取两个列日期? I want the "starttime" column to compare with "created_at". 我希望“starttime”列与“created_at”进行比较。 Is this achievable? 这可以实现吗? Here is my code so far: 到目前为止,这是我的代码:

$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; 

But it gives me an error: 但它给了我一个错误:

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

Also how can i give the array to carbon. 另外,我如何将阵列赋予碳。

So finally here is the thing i tried: 所以最后这是我尝试的事情:

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


       echo $diff;

But when ever i refresh the page, the value changes in seconds. 但是当我刷新页面时,值会以秒为单位变化。 and if another record is added, it adds up again. 如果添加了另一条记录,它会再次累加。

Pluck will give you an array of all of the start times from your result set which is why you're passing an array into parse . Pluck将为您提供结果集中所有开始时间的数组,这就是您将数组传递给parse You're actually getting all of the start times and all of the created ats then trying to compare all to all, effectively. 你实际上得到了所有的开始时间和所有创建的ats,然后尝试有效地比较所有。

You either need to get a single result, 你要么得到一个结果,

Like 喜欢

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

Or if you wanted it for each row you'd have to iterate over them and do much the same 或者,如果你想要每行,你必须迭代它们并做同样的事情

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
});

The other thing you could do is put an accessor on your Clean model to help you out with this 您可以做的另一件事是在Clean模型上放置一个访问器来帮助您解决这个问题

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

Then 然后

echo Clean::first()->diffInHours;

Or 要么

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

Also, if you add the following to your model, Eloquent will automatically parse the strings into Carbon objects so you can skip the need for Carbon::parse() in your code 此外,如果您将以下内容添加到模型中,Eloquent将自动将字符串解析为Carbon对象,以便您可以在代码中跳过对Carbon :: parse()的需求

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

Try adding protected $dates to your Clean model, like this: 尝试将protected $dates添加到Clean模型中,如下所示:

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

As you can read from the comments inside the code, put all of the columns that should be converted to dates inside, this will help you achieve date manipulations easier. 正如您可以从代码中的注释中读取的那样,将所有应转换为日期的列放入其中,这将有助于您更轻松地实现日期操作。

EDIT 1: 编辑1:

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

Then you can compare like this: 然后你可以像这样比较:

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

EDIT 2: 编辑2:

In order for the code underneath to work, you must initialize $start and $finish dates as in EDIT 1 为了使下面的代码有效,您必须在EDIT 1初始化$start$finish日期

$ago = $start->diffForHumans($finish, true); //2 days OR 3 weeks OR 1 hour

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM