megamage
megamage

Reputation: 346

How to Display the Difference Between Two Dates in laravel 4

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.

Upvotes: 2

Views: 8148

Answers (3)

Gamitha Anjana
Gamitha Anjana

Reputation: 1

<?php 
  $to_date = Carbon::parse($datetime1);
  $from_date = Carbon::parse($datetime2);

  $display_diff = $to_date->from($option = $from_date);
?>

Upvotes: 0

ErcanE
ErcanE

Reputation: 1641

<?php 
    $datetime1 = new DateTime(($variables->dateFrom));
    $datetime2 = new DateTime(($variables->dateTo));
    $interval = $datetime1->diff($datetime2);
    echo $interval->format('%R%a days');
?>

No need to use date_diff ,try to use more Object Oriented way! Add this code inside Laravel Blade Engine DONE!

Upvotes: 1

Matthew A Thomas
Matthew A Thomas

Reputation: 914

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;

Upvotes: 3

Related Questions