This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
I have two date's
$date1 = "2014-02-11 04:04:26 AM"
$date2 = "2014-02-11 05:36:56 AM"
I want to calculate the difference and display it as follows
1 hour 32 minutes
Make use of DateTime::diff of the DateTime Class
<?php
$datetime1 = new DateTime('2014-02-11 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";
OUTPUT :
1 Hours 32 Minutes
Simply convert both dates to timestamp if dont want to do it in complex way...
Something like this
$dateDiff = intval((strtotime($date1)-strtotime($date2))/60);
$hours = intval($dateDiff/60);
$minutes = $dateDiff%60;
and there you go...
Thank you...
Related
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 4 years ago.
how can I get difference between 2 time in hours.
For ex:
$data1 = '2018-04-24 02:30:00';
$date2 = now();
how to get diff between $date1 and $date2.
EDIT: code posted by OP in comments
<?php //date_default_timezone_set('UTC+6');
$time1 = strtotime('2018-04-25 12:00:00');
$time2 = time();
echo $time2.'<br>';
echo date('Y-m-d h:i:s', $time2).'<br>';
echo ($time1-$time2)/3600; ?>
<?php
$datetime1 = new DateTime('2018-04-24 02:30:00');
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%Y %m %d %H:%I:%S');
The result will be:
00 0 1 08:06:15
00 --> years
0 --> months
1 --> days
08 --> hours
06 --> minutes
15 --> seconds
You can modify it as you like but i suggest you keep at least days cause the hours may differ by a few hours but the days can differ by many days.
$data1 = '2018-04-24 02:30:00';
$data2 = date('y-m-d H:i:s');
$formated_in = date('Y-m-d H:i:s', strtotime($data1));
$formated_out = date('Y-m-d H:i:s', strtotime($data2));
$formated_new_in = strtotime($formated_in);
$formated_new_out = strtotime($formated_out);
$sub_total = $formated_new_out - $formated_new_in;
$sub_total = gmdate("H:i", $sub_total);
echo $sub_total;
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
I have two dates assigned in variable $date1 and $date2..
Here is the code..
if (isset($_POST['check_in']))
{
$date1=date('Y-m-d', strtotime($_POST['check_in']));
}
if (isset($_POST['check_in']))
{
$date2=date('Y-m-d', strtotime($_POST['check_out']));
}
For example if date1="2015-05-21" and date2="2015-05-23".I want the difference of date as 2
Use DateTime class. Try with -
$date1=new DateTime("2015-05-21");
$date2=new DateTime("2015-05-23");
$interval = $date1->diff($date2);
echo $interval->format('%R%a days');
Output
+2 days
DateTime()
Since strtotime returns unixtime, the difference in seconds can be calculated by simply subtracting the one strtotime from the other:
$seconds = strtotime($_POST['check_out']) - strtotime($_POST['check_in']);
Then to find the days:
$days = $seconds / 60 / 60 / 24;
Here you go:
https://php.net/manual/en/datetime.diff.php
Code with various examples.
Here's one I like:
<?php
$datetime1 = date_create('2015-05-21');
$datetime2 = date_create('2015-05-23');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
I hope this helps :)
This question already has answers here:
Finding the number of days between two dates
(34 answers)
Closed 8 years ago.
I've always used the code below to calculate the difference (in days) between two dates. But I'm wondering if there is something more intelligent, someone knows a better way to do this?
//86400 one day
$data1 = '2013-07-24 21:30:00';
$data2 = '2013-08-24 21:30:00';
$diff = (abs(strtotime($data1) - strtotime($data2))) / 86400;
var_dump($diff);
You can use DateTime:diff:
$data1 = new DateTime('2013-07-24 21:30:00');
$data2= new DateTime('2013-08-24 21:30:00');
$interval = $data1->diff($data2);
echo $interval->format('%R%a days');
You can also use DateTime::createFromFormat for better result in case that your input date has different format, for example:
$date1 = DateTime::createFromFormat('Y-m-d H:i:s', '2013-07-24 21:30:00');
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2013-08-24 21:30:00');
$interval = $date1->diff($date2);
echo $interval->format('%R%a days');
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate the difference between two dates using PHP?
Date Difference in php?
I have two dates in a variable like
$fdate = "2011-09-01"
$ldate = "2012-06-06"
Now I need the difference in months between them.
For example, the answer should be 10 if you calculate this from month 09 (September) to 06 (June) of next year - you'll get 10 as result.
How can I do this in PHP?
A more elegant solution is to use DateTime and DateInterval.
<?php
// #link http://www.php.net/manual/en/class.datetime.php
$d1 = new DateTime('2011-09-01');
$d2 = new DateTime('2012-06-06');
// #link http://www.php.net/manual/en/class.dateinterval.php
$interval = $d2->diff($d1);
$interval->format('%m months');
Have a look at date_diff:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%m months');
?>
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Date Difference in php?
Compare 2 timestamps in PHP and get the days between?
I've 2 dates like 15/02/2012 and 18/03/2012, I need to know the number of days difference between them. Does such a utility exist in php?
date_diff is what you want.
$diff = (int) ( abs(strtotime($date1) - strtotime($date2)) / (60 * 60 * 24) );
echo 'diff:' . $diff
PHP has a function for this:
http://www.php.net/manual/en/datetime.diff.php
Example
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Output
+2 days
You could turn those dates into timestamps:
$ts1 = strtotime("15-02-2012");
$ts2 = strtotime("18-03-2012");
$diff = ($ts2 - $ts1)/86400;
echo $diff . " days";