I'm making a calendar of events and showing, with the help of Carbon , the dates from the most recent to occur with the diffForHumans()
Carbon function .
I think I know exactly what the problem is but I would like some advice on the best way to handle the dates.
In the base I save it as String
, since I Date
only saved the day and I needed the combo.
But of course at the moment of bringing the events and ordering them by "date" it is aString
$eventos= Evento::orderBy('fecha','asc')->get();
It is clear that this will not order me correctly. Why order String
. How do I fix this?
Scholarship Marathon / 09.10.2016 14:00
Carussi Marathon / 10.08.2016 15:00
Bovril Marathon / 10.08.2016 09:51
Health Marathon / 09.30.2016 16:00
Being today 09/30 at 12:00 it should show the Health Marathon first, but it shows it last. After if it seemed ordered.
It also happens to me that when I use the Carbon function it is adding 3 hours to me (I suppose it has something to do with the GTM) diffForHumans()
. So if there are 4 hours left, it is telling me that there is 1 hour left. This is how I use it:
@foreach($eventos as $evento)
@if($evento->fecha >= Carbon\Carbon::now())
<h5>
<a href="#">
{{$evento->nombre}} . {{$evento->distancia}}<br>
{{ Carbon\Carbon::createFromFormat('Y-m-d H:i:s',$evento->fecha)->diffForHumans() }}
{{Carbon\Carbon::now()}}
</a>
</h5>
@endif
@endforeach
You may also be having some conversion problem when creating the date with Carbon from the String
.
Changing the time zone should fix the time skew problem, using the
setTimezone('America/New York')
eg method, considering that diffForHumans() takes this into account: