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";
Related
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
I have two times, like this:
$date1 = strtotime("02/12/2019 10:10:54 am");
$date2 = strtotime("02/12/2019 10:11:07 pm");
$diff = abs($date2 - $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));
printf(" %d days, %d hours, "
. "%d minutes, %d seconds",
$days, $hours, $minutes, $seconds);
I want to calculate the difference between them, but it's not working.
This is much easier with the DateTime class, which has a diff method which produces a DateInterval object that can output the difference in whatever format you like:
$date1 = new DateTime("02/12/2019 10:10:54 am");
$date2 = new DateTime("02/12/2019 10:11:07 pm");
$diff = $date1->diff($date2);
echo $diff->format('%y years, %m months, %d days, %h hours, %i minutes and %s seconds');
Output:
0 years, 0 months, 0 days, 12 hours, 0 minutes and 13 seconds
Demo on 3v4l.org
I want to subtract one date to another using php and display result in the days - hours - min - sec format. How i do this using php . I tried this using time stamp but it does not give proper values. please give suggestion
For ex : from date 2012-04-27 19:30:56 to date 2012-04-27 19:37:56
i used this code
if(strtotime($history['datetimestamp']) > strtotime($lasttime)) {
$totalelapsed1 = (strtotime($history['datetimestamp'])-strtotime($lasttime));
if($totalelapsed1 > 60 ) {
$sec = $totalelapsed1%60;
$min = round($totalelapsed1/60 , 0);
$minute = $min + $minute;
$second = $sec + $second;
// echo $min. " min " .$sec." sec";
} else {
//echo "0 min " . $totalelapsed1." sec";
$minute = 0 + $minute;
$second = $totalelapsed1 + $second;
}
} else {
$minute = 0 + $minute;
$second = 0 + $second;
// echo "0 min 0 sec";
}
From how to subtract two dates and times to get difference by VolkerK:
You have to use like this:-
<?php
//$now = new DateTime(); // current date/time
$now = new DateTime("2010-07-28 01:11:50");
$ref = new DateTime("2010-07-30 05:56:40");
$diff = $now->diff($ref);
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);
prints 2 days, 4 hours, 44 minutes
see http://docs.php.net/datetime.diff
edit: But you could also shift the problem more to the database side, e.g. by storing the expiration date/time in the table and then do a query like
... WHERE key='7gedufgweufg' AND expires<Now()
Many rdbms have reasonable/good support for date/time arithmetic.
Link Url:- how to subtract two dates and times to get difference
http://www.webpronews.com/calculating-the-difference-between-two-dates-using-php-2005-11
I suggest to use DateTime and DateInterval objects.
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "days difference ".$interval->d." days ";
read more php DateTime::diff manual
Try using DateTime:diff().
Examples are provided on that page.
This script resolve my issue
$date1 = date("d-m-Y H:i:s",strtotime($date1));
$date2 = date("d-m-Y H:i:s",strtotime($lasttime));
$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)); $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));
printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds);
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
How to find time difference between two dates using PHP.
For example i am having two dates:
start Date : 2010-07-30 00:00:00
end Date : 2010-07-30 00:00:00
In this case how shall i find the time difference using PHP.
But i need like following : 24hrs 3 minutes 5 seconds
If you're using PHP 5.3 or better (which you should be), you can use the built in DateTime class to produce a DateInterval which can be formatted easily.
$time_one = new DateTime('2010-07-29 12:43:54');
$time_two = new DateTime('2010-07-30 01:23:45');
$difference = $time_one->diff($time_two);
echo $difference->format('%h hours %i minutes %s seconds');
DateTime was introduced in 5.1, but DateInterval is new to 5.3.
$d1 = strtotime('2010-07-30 00:00:00');
$d2 = strtotime('2010-07-30 00:00:02');
$diff = $d2 - $d1;
echo $diff;
You will have second in $diff variable
<?php
$date1 = $deal_val_n['start_date'];
$date2 = $deal_val_n['end_date'];
$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));
$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));
?>
Try following code,
<?php
$date1 = $deal_val_n['start_date'];
$date2 = $deal_val_n['end_date'];
$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));
$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));
?>