0

I tried to get the difference from two dates with this code

<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>

Is there a way to return an integer instead of a formatted string?

Thanks :)

domihein
  • 23
  • 3

2 Answers2

1

Use strtotime() to convert date into timestamp and then subtract the dates and divide the difference with 60*60*24, you will gets days as integer value ..

<?php 
$date1=strtotime("2013-03-15");
$date2=strtotime("2013-12-12");
$diff= $date2-$date1;
echo floor($diff/(60*60*24));
?>
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
0

you can choose any of following

Type Casting

 echo (int) $diff->format("%R%a");

another way using PHP intval()

  echo intval($diff->format("%R%a"));
AkshayP
  • 2,141
  • 2
  • 18
  • 27