This is my code:
$logdate = Input::get('logdate');
$enddate = Input::get('enddate');
//Cast the dates to yyyy-dd-mm format for comparison
$startdt = date('Y-d-m',strtotime($logdate));
$enddt = date('Y-d-m',strtotime($enddate));
//Cast the dates into datetime objects
$date1 = new DateTime($startdt);
$date2 = new DateTime($enddt);
//Calculate the difference between the 2 datetime objects
$diff = $date1->diff($date2, true);
//cast logdate into correct format for entry in the database
$newlogdate = strtotime($logdate);
$formatlogdate = date('Y-d-m',$newlogdate);
I'm using this to get the amount of days in the interval:
Log::info(intval($diff->days));
The code here works fine whenever I write code that involves 12 day differences or less withhin the same month but as soon as it's more than 12 days or as soon as I try to calculate differences between 2 months it goes haywire and gives me 16000+ as a result
for example according to this code the difference in days between 26/04/2015 and 02/05/2015 is 16557 days, I don't know how they get to this number but it's around 45 years, and the result is always 16000 + no matter which dates I pick given they are outside the 12 day single month radius
When you are doing - date('Y-d-m',strtotime($logdate)); it is returning - 1970-01-01 for both the dates.
/ is creating the problem.
Try this -
$log = '26/04/2015';
$end = '02/05/2015';
$date1 = new DateTime(str_replace('/', '-', $log));
$date2 = new DateTime(str_replace('/', '-', $end));
$diff = $date1->diff($date2, true);
echo $diff->days;
Output
6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With