How to Display the Difference Between Two Dates in laravel 4

megamage picture megamage · Dec 10, 2014 · Viewed 7.1k times · Source

Hi i am creating an application using laravel, and i want to display the difference between two different dates in laravel. Imean the exact replica of date_diff used in php for eg

date_diff($date1,$date2);

When i try to use this in my controller it shows class/controller/datetime function cannot be used.

I want to use this function in my controller how do i achieve this.I heard of using carbon as i am new to this i do know how to achieve this functionality by using carbon.If possible please explain with an example.

Answer

Matthew A Thomas picture Matthew A Thomas · Dec 10, 2014

Not an expert in laravel but you could use php built in datetime class to do this e.g.

  $datetime1 = new DateTime();
  $datetime2 = new DateTime('2014-01-03 17:13:00');
  $interval = $datetime1->diff($datetime2);
  $elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %S seconds');
  echo $elapsed;