Reputation: 1421
So, I have this view in my controller to show a button if the current date lies between the start date and the end date. Here is what I have done so far :
@if (Carbon\Carbon::now()->format('Y-m-d') < Carbon\Carbon::parse($edition->start)->format('Y-m-d') && Carbon\Carbon::now()->format('Y-m-d') > Carbon\Carbon::parse($edition->end)->format('Y-m-d'))
<p></p>
@else
<div class="tombol-nav">
<a href="../journal/create?edition={{$edition->id}}" class="btn btn-primary">Upload Jurnal Anda Sekarang!</a><br>
<p style="color: red;">Penting! Batas waktu terakhir pengunggahan Jurnal pada Edisi ini : {{ Carbon\Carbon::parse($edition->limit)->format('j F, Y') }}</p>
</div>
@endif
I don't know what's wrong with it, the button will still appear even though the end date has passed. Thank you for your help.
Upvotes: 2
Views: 2216
Reputation: 1421
So I finally did it with someone help from another forum, I'm gonna write it down here in case some people need it.
@if (Carbon\Carbon::now()->between(Carbon\Carbon::parse($edition->start), Carbon\Carbon::parse($edition->limit)))
<div class="tombol-nav">
<a href="../journal/create?edition={{$edition->id}}" class="btn btn-primary">Upload Jurnal Anda Sekarang!</a><br>
<p style="color: red;">Penting! Batas waktu terakhir pengunggahan Jurnal pada Edisi ini : {{ Carbon\Carbon::parse($edition->limit)->format('j F, Y') }}</p>
</div>
@else
<p></p>
@endif
Upvotes: 2
Reputation: 382
An easier way to deal with date comparison in Blade is to use Carbon's diffInSeconds()
For eg:
@if(\Carbon\Carbon::now()->diffInSeconds($edition->start, false) > 0 )
//$edition->start is in the future
@else
//$edition->start is in the past
@endif
See here for more information: http://carbon.nesbot.com/docs/#api-difference
So, your code could be rewritten as
@if (Carbon::now()->diffInSeconds($edition->start) < 0 && Carbon::now()->diffInSeconds($edition->end, false) > 0)
<p></p>
@else
<div class="tombol-nav">
<a href="../journal/create?edition={{$edition->id}}" class="btn btn-primary">Upload Jurnal Anda Sekarang!</a><br>
<p style="color: red;">Penting! Batas waktu terakhir pengunggahan Jurnal pada Edisi ini : {{ Carbon\Carbon::parse($edition->limit)->format('j F, Y') }}</p>
</div>
@endif
Depending on the precision that you need, you could also use diffInMinutes()
or similar methods.
Upvotes: 0
Reputation: 115
If condition does not work in case of objects.
Carbon\Carbon::now()->format('Y-m-d') returns an object.You have to convert it into string and then pass it in the if condition.
Convert carbon object into string : -
$carbonDateTimeObject = Carbon\Carbon::now()->toDateTimeString();
Upvotes: 0
Reputation: 2098
Please use this code as reference to check date between two dates in laravel using carbon.
$first = Carbon::create(2012, 9, 5, 1);
$second = Carbon::create(2012, 9, 5, 5);
var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second)); // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false)); // bool(false)
Also read more about Carbon here : http://carbon.nesbot.com/docs/
Upvotes: 0