Drennos
Drennos

Reputation: 313

Laravel: display difference between two dates in blade

I want to know if it is possible to show in a blade view the difference between the system date and a record of the database with date format ($ticket->start_date) without using Carbon.

For example (I know, it is not working)

<td>{{ diff(date('Y-m-d'), strtotime($ticket->start_date))}}</td>

Upvotes: 1

Views: 11784

Answers (3)

Tharaka Dilshan
Tharaka Dilshan

Reputation: 4499

function

date_diff()

Parameters

There are Two paramers.
You need to pass DateTmime objects to those parameters.

Returns

This function return a DateInterval object.
You can format DateInterval object using format() function.

Example

<td>{{ date_diff(new \DateTime($ticket->start_date), new \DateTime())->format("%m Months, %d days"); }}</td>

you can type anything in this format() string.

this example format returns something like "9 Months, 18 days"

DateInterval Object

this is an example of DateInterval object.
you can access these properties via format() function posted as below.

  interval: + 9m 18d 02:27:21.488037
  +"y": 0
  +"m": 9
  +"d": 18
  +"h": 2
  +"i": 27
  +"s": 21
  +"f": 0.488037
  +"weekday": 0
  +"weekday_behavior": 0
  +"first_last_day_of": 0
  +"invert": 0
  +"days": 291
  +"special_type": 0
  +"special_amount": 0
  +"have_weekday_relative": 0
  +"have_special_relative": 0

Upvotes: 6

alexandru.gaidei
alexandru.gaidei

Reputation: 69

Please use Carbon lib which is part of laravel. https://carbon.nesbot.com/docs/#api-difference

dd( \Carbon\Carbon::now()->diff( \Carbon\Carbon::parse('your db datetime') ) );

Example:

dd( \Carbon\Carbon::now()->diff( \Carbon\Carbon::parse('2019-12-12 00:00:00') ) );

Upvotes: 3

haz
haz

Reputation: 1636

You've got two separate problems here:

First: how do you diff two dates. You can go high-tech or low-tech here. If you don't want to use Carbon, I suggest going low-tech:

<?php
// Note, this gives you a timestamp, i.e. seconds since the Epoch.
$ticketTime = strtotime($ticket->start_date);

// This difference is in seconds.
$difference = $ticketTime - time();

At this point, you've got to decide how you want to output the difference. In seconds? In hours?

Difference: {{ $difference }} seconds

Difference: {{ round($difference / 3600) }} hours

Difference: {{ round($difference / 86400) }} days

You'll have to do extra engineering if you want something as pretty as Carbon::diffForHumans().

Second: This now becomes a question for you whether this is too much code for your front-end. Obviously you could reduce all of the above to a one-liner in PHP, but your co-workers may not appreciate how unreadable it becomes:

{{ round((strtotime($ticket->start_date) - time()) / 3600) }} hours

Caveats

Using timestamps ducks the issue of dealing with timezones. For a bunch of use cases, this is sufficient; for others this is woefully inadequate. But if you want to deal with timezones, you're definitely better off using Carbon, which is better than using PHP's DateTime, but up to you.

Upvotes: 7

Related Questions