mouse_ua
mouse_ua

Reputation: 17

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.

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? Because in the end I get a negative value of -17 hours. 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:

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

$realTime for date now give me:

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

Upvotes: 0

Views: 6129

Answers (2)

Qasim Nadeem
Qasim Nadeem

Reputation: 607

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

Upvotes: 3

user379888
user379888

Reputation:

You should grab the difference in seconds using Carbon and then use its fields to get your work done. Source.

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

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

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

gmdate('s', $totalDuration);

Upvotes: 0

Related Questions