PHP DateDiff issue - php

Using the PHP code below, I would expect to get '2' as my output. But I get '1'.
Does anyone know why this is?
$returndate = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3-$2-$1', '2011-03-28');
$departdate = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3-$2-$1', '2011-03-26');
$diff = abs(strtotime($returndate) - strtotime($departdate));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
echo $days; // expecting 2, but get 1
Many thanks for any help.

So much calculations... I assume its a rounding issue you have, rounding all the time measurements... here is a simpler look on what you are doing:
function dateDiff($start, $end) {
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$diff = $end_ts - $start_ts;
return round($diff / 86400);
}

$d1 = new DateTime('2011-03-28');
$d2 = new DateTime('2011-03-26');
echo $d1->diff($d2)->d;
Output: 2

Related

time difference between 2 values while if second value is past the current time

how to get the time difference between 2 values while if second value is past the current time?
i use below code:
function time_diff($date2, $date1, $format){
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24) / (60*60));
$minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));
if ($format == 'year'){
$i = $years;
}elseif ($format == 'month'){
$i = $months;
}elseif ($format == 'day'){
$i = $days;
}elseif ($format == 'hour'){
$i = $hours;
}elseif ($format == 'minutes'){
$i = $minutes;
}elseif ($format == 'second'){
$i = $seconds;
}
return $i;
}
i want to get time difference between (saved time in database) and (current time).
everything is fine, but when (saved time in database) passes and its smaller than (current time), this code return value but without negative sign.
i mean if time difference is 2 day, it return 2 while it should return (-2) or somethings like that cuz (saved time in database) passes and its smaller than (current time).
you used $diff = abs(strtotime($date2) - strtotime($date1));, while abs() stands for absolute (as in the result is always positive) using strtotime($date2) - strtotime($date1) should work instead

Find difference between time in php [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 6 years ago.
I need to find the different between 2 timestamp values ,how to calculate it
$time1 ='2016-08-31T07:00:00';
$time2 ='2016-08-31T08:45:00';
I need output time value as 1h 45m
How to get desired output
<?php
$time1 ='2016-08-31T07:00:00';
$time2 ='2016-08-31T08:45:00';
$d = strtotime($time2) - strtotime($time1);
echo gmdate("g", $d),'h ',gmdate('i',$d),'m ';
$date_a = new DateTime('2016-08-31T07:00:00');
$date_b = new DateTime('2016-08-31T08:45:00');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%h Hours:%i Min:%s sec');
You Can test the code Here
Use strtotime() to convert datetime in timestamps and get the difference between 2 times.
Now, Divide the difference into desire format. Use below code.
$time1 ='2016-08-31T07:00:00';
$time2 ='2016-08-31T08:45:00';
$diff = abs(strtotime($time1) - strtotime($time2));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
$minuts = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
echo $hours . "h " .$minuts."m";
Output
1h 45m
Live Demo : Click Here
Use this code,
$datetime1 = strtotime("09:00");
$datetime2 = strtotime($items['duration']);
$interval = abs($datetime2 - $datetime1);
$min = round($interval / 60);
$d = floor ($min / 1440);
$h = floor (($min - $d * 1440) / 60);
$m = $min - ($d * 1440) - ($h * 60);
if (!$m) {
$m = '00';
}
echo $h."h:".$m."m";

Calculate between two day and calculate a fine

$date2 = $row['returnbefore'];
$date1 = date('Y/m/d');
$diff = abs(strtotime($date1) - strtotime($date2));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$final = ($days * 10) ;
echo $final;
i want to do a library system when user return book will check the return date between current date and calculate a fine to user which multiply day by 10 .
what step i left ? and i wan set the current time is my computer time so i can test it
This is a fairly simple problem to accomplish using DateTime and DateInterval.
$now = new DateTime();
$dueDate = new DateTime($row['returnbefore']);
$lateInterval = $now - $dueDate;
$daysLate = $lateInterval->format('d');
$fine = $daysLate > 0 ? intval(floor($daysLate)) * 10 : 0; // This means that you are not charged for a late day until the end of the day

how to get a difference in dates, when the dates is passed through post variable to another page and echo the difference in php

I tried with the below code to find the difference between two dates which is passed through post variable and print, but failed.
$fromdate=$_POST['from_date'];
$todate=$_POST['to_date'];
$date1 = new DateTime($fromdate); //inclusive
$date2 = new DateTime($todate); //exclusive
$diff = $date2->diff($date1);
echo $diff;
Something like this should work for you:
<?php
$_POST['from_date'] = "2014-10-01";
$_POST['to_date'] = "2014-11-02";
$fromdate = $_POST['from_date'];
$todate = $_POST['to_date'];
$diff = abs(strtotime($fromdate) - strtotime($todate));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf("%d years, %d months, %d days\n", $years, $months, $days);
?>
Output:
0 years, 1 months, 2 days

convert year or month into days, PHP

So I have used this method to get the difference between 2 dates.
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
Now, lets say that I want to convert the years and months into days. How do I do that?
Using DateTime this is a piece of cake:
$date1 = new DateTime($date1);
$date2 = new DateTime($date2);
$diff = $date1->diff($date2, true);
echo $diff->format('%a') . ' days';
$currentDate = date("d-m-Y");
$date1 = date_create("".$joining_date."");
$date2 = date_create("".$currentDate."");
$diff12 = date_diff($date2, $date1);
$hub_days = $diff12->days;
$months = $diff12->m;
$years = $diff12->y;

Categories