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));
?>
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
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";
Hi I'm trying to to do the following with php and html:
I have a form that submits time() and stores it as the variable TIMESTAMP in $POST, the next time the form is submitted a new variable is posted called DELAY, which is time()-TIMESTAMP. The process seems to work, but my problem appears to the PHP time() function seems to be a bit fluttery.
I can submit the form every lets say 3 secs and sometimes DELAY = 3 as it should other times its not correct like say 7 secs or even 12 secs. How can I get the time() function to be more accurate when returning the current time?
I need to use the delay in an MySQL query that is only executed when the delay is greater than 15 secs
This is surely not php time() related.
If you're running on a virtual machine, you can experience time drifts if host is not configured properly. Check if this is the case.
i hope this code help you
function DifTime($T1,$T2){
$diff=abs($T2-$T1);
$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));
return array('Day'=>$days,'Month'=>$months,'Year'=>$years,'Hours'=>$hours,'Minuts'=>$minuts,'Seconds'=>$seconds);
}
sample:
$Data=DifTime(TIMESTAMP,(time()-TIMESTAMP)+TIMESTAMP);
printf("%d:%d:%d:%d", $Data['Day'], $Data['Hours'], $Data['Minuts'], $Data['Seconds']);
output:
0:0:0:10
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;