This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I have two time values as follows
$start = '13:00:00';
$end = '21:00:00';
I want calculate the difference between these two time values, I want difference value in format like 00:00:00 (here result would be 08:00:00)
I am using below code to calculate difference
$time = date( "h:i:s", strtotime($end) - strtotime($start));
but it gives me result as 01:30:00
please help if anyone have any idea, i do not want to use DateTime class
In my opinion it is not duplicate, if it is tell me the answer if your
can find in duplicate question
$datetime1 = new DateTime('13:00:00');
$datetime2 = new DateTime('21:00:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%H:%I:%S');
Try with this code :
$time1 = strtotime('13:00:00');
$time2 = strtotime('21:00:00');
$diff = $time2 - $time1;
echo 'Difference: '.date('H:i:s', $diff);
Its easier to do that with the DateTime::diff function.
There is an example an that site.
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Related
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:
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...
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";