Laravel: display difference between two dates in blade
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.
I'm using laravel, I want to calculate the days between two dates in store function in the controller and show in the index view automatically
Since you are new to laravel .Here is the answer.I assume date format of date_from
and date_to
is month/date/year
$dateFrom=\Carbon\Carbon::createFromFormat('m/d/Y',$request->date_from);
$dateTo=\Carbon\Carbon::createFromFormat('m/d/Y',$request->date_to);
if($dateTo>$dateFrom){
dd($dateFrom->diffInDays($dateTo));
}
The above code will return number of days between two dates
Updated
public function store(Request $request)
{
$dateFrom=\Carbon\Carbon::createFromFormat('m/d/Y',$request->date_from);
$dateTo=\Carbon\Carbon::createFromFormat('m/d/Y',$request->date_to);
Application::create([
'user_id'=>auth()->user()->id,
'leave_id'=>$request->leave_id,
'date_from'=>$request->date_from,
'date_to'=>$request->date_to,
'days'=> $dateFrom->diffInDays($dateTo),
]);
return redirect()->route('admin.leaverequest.index')
->with('success', 'Leave Application Submitted');
}
Comparing two dates in blade.php file
@php
use Carbon\Carbon;
$today_date = Carbon::now();
foreach ($entries as $k => $entry) {
$expire_date = Carbon::createFromFormat('Y-m-d', $entry->expire_date);
$data_difference = $today_date->diffInDays($expire_date, false); //false param
if($data_difference > 0) {
//not expired
}
elseif($data_difference < 0) {
//expired
} else {
//today
}
}
@endphp
The false
parameter is optional and indicates if you want the return value to be the absolute value or a relative value that might have a - (negative) sign if the passed in date is less than the current instance. This will default to true, return the absolute value.
More here
Finding days between two dates in laravel
You don't need Carbon, you can do that using simple PHP. Best is to use PHP OOP way. Like this:
$fdate = $request->Fdate;
$tdate = $request->Tdate;
$datetime1 = new DateTime($fdate);
$datetime2 = new DateTime($tdate);
$interval = $datetime1->diff($datetime2);
$days = $interval->format('%a');//now do whatever you like with $days
PS : DateTime is not a function, it's a class, so don't forget to add : use DateTime;
at top of your controller so that it can reference to right root class, or use \DateTime()
instead when making its instance.
How to calculate difference between two dates as day month year using carbon in laravel
I found a short and simple way
$time = Carbon::now()->diff($row->entry_date);
return '<td>' . $time->y . ' Year' . $time->m . ' Month' . $time->d . ' Day' . '</td>';
Finding numbers of days between 2 dates in Laravel blade
you have to use Carbon and calculate it in your controller like this
import the carbon namespace in your controller
use Carbon\Carbon;
and then calculate the date difference like this
$date = Carbon::parse($user->paid_for_date);
$now = Carbon::now();
$diff = $date->diffInDays($now);
and return the $diff to the view
Calculate difference between two dates using Carbon and Blade
You are not following the example from the Carbon Documentation. The method Carbon::createFromDate()
expects 4 parameters: year, month, day and timezone. And you are trying to pass a formatted date string.
If you want to create a Carbon object from a formatted date string you can use the constructor of the class just like this:
$date = "2016-09-17 11:00:00";
$datework = new Carbon($date);
Or you can use the static Carbon::parse()
method:
$date = "2016-09-17 11:00:00";
$datework = Carbon::parse($date);
For your purposes you can use the this full example:
$date = Carbon::parse('2016-09-17 11:00:00');
$now = Carbon::now();
$diff = $date->diffInDays($now);
And then in your Blade template:
<td> {{ $diff }} </td>
Laravel Carbon differents between two dates
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);
Related Topics
Laravel, Sync() - How to Sync an Array and Also Pass Additional Pivot Fields
How to Use PHP'S Password_Hash to Hash and Verify Passwords
How to Get Woocommerce Order Details
How to Combine Two Strings Together in PHP
How to Sort a Multidimensional Array in PHP
Are PHP Short Tags Acceptable to Use
How to Prevent Xss With Html/PHP
How to Catch a PHP Fatal ('E_Error') Error
How to Send an Email Using PHP
PHP Execute a Background Process
Send Attachments With PHP Mail()
Reference - How to Handle Namespaces (Tags and Attributes With a Colon in Their Name) in Simplexml
Converting Timestamp to Time Ago in PHP E.G 1 Day Ago, 2 Days Ago...
Php Substring Remove Everything in a String After Certain Character
Laravel, Failed to Authenticate on Smtp Server With Username "" Using 3 Possible Authenticators