简体   繁体   English

Laravel 两个日期之间的碳差异

[英]Laravel Carbon differents between two dates

I have weekly discounts on the product on my site, when buying, the user is written how much the discount will still be valid.我的网站上的产品每周都有折扣,购买时,用户会写下多少折扣仍然有效。 There is a problem in calculating the time between hours and minutes.计算小时和分钟之间的时间时出现问题。

In order to calculate how many days there will be a discount, I do it like this:为了计算有多少天会有折扣,我这样做:

$realTime = Carbon::now();
$diff = $realTime->diffInDays($item->created_at);
$date = 7 - $diff;
    if ($diff < 7) {
        Notify::create(
            [
            'user_id' => $this->user->id,
            'title' => 'Message',
            'message' => 'Discount will be available '.$date.' '.Lang::choice('day|days|', $date, [], 'ru'),'',
            'status' => '2',
        ]);
    }

This is returned to me by the inscription that the discount will be available for another 1, 2, 3... days.这是通过题词返回给我的,折扣将在另外 1、2、3... 天可用。

And now, how i can calculate the remaining hours and minutes?现在,我如何计算剩余的小时数和分钟数?

I try something like this:我尝试这样的事情:

$realTime = Carbon::now();
$diff_hours = $realTime->diffInHours($item->created_at);
$date_hours = 168 - $diff_hours;
    if ($diff == 7) {
        Notify::create(
            [
            'user_id' => $this->user->id,
            'title' => 'Message',
            'message' => 'Discount will be available '.$date_hours .' '.Lang::choice('hour|hours|', $date_hours , [], 'ru'),'',
            'status' => '2',
        ]);
    }

168 it's hours at 1 week, but maybe I'm doing something wrong? 168是第 1 周的时间,但也许我做错了什么? Because in the end I get a negative value of -17 hours.因为最后我得到-17小时的负值。 And by the same method, I try to make a difference in minutes, but again I get a negative and incomprehensible value.并且通过同样的方法,我试图在几分钟内做出改变,但我再次得到一个负面和难以理解的价值。

UPD : at my database item has format 2020-05-26 23:40:15 i write: UPD :在我的数据库中,项目的格式2020-05-26 23:40:15我写道:

$realTime = Carbon::now();
$diff = $realTime->diffInDays($item->created_at);

$realTime for date now give me: $realTime for date 现在给我:

object(Carbon\Carbon)#623 (3) { ["date"]=> string(26) "2020-05-30 18:51:23.846522" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Moscow" }

suppose you have your start and end discount dates like this:假设您有这样的开始和结束折扣日期:

$start  = new Carbon('2018-10-04 15:00:03');
$end    = new Carbon('2018-10-05 17:00:09');

you can do like this:你可以这样做:

$diff = $start->diff($end);

now $diff will give you full control on days and minutes.现在 $diff 将让您完全控制天数和分钟数。 you can do following for getting days:您可以为获取天数执行以下操作:

$diff->d 

for minutes几分钟

$diff->m 

means you can concatenate values to create a statement like this:意味着您可以连接值以创建如下语句:

$diff->d . 'days ' . $diff->m . 'minutes and ' . $diff->s . ' seconds remaining.'

final testing code may look like this:最终测试代码可能如下所示:

$start  = new Carbon('2018-10-04 15:00:03');
        $end    = new Carbon('2018-10-05 17:00:09');
        $diff = $start->diff($end);
        $message = $diff->d . 'days ' . $diff->m . 'minutes and ' . $diff->s . ' seconds remaining.';

        dd($message);

You should grab the difference in seconds using Carbon and then use its fields to get your work done.您应该使用 Carbon 在几秒钟内获取差异,然后使用它的字段来完成您的工作。 Source .来源

$totalDuration = $finishTime->diffInSeconds($startTime);

gmdate('H', $totalDuration);//hours

gmdate('m', $totalDuration); // minutes

gmdate('s', $totalDuration);

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

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