How to calculate difference between the dates in PHP? [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to calculate the difference between two dates using PHP?
How to calculate difference between the dates?
Is there any default function for to calculate the date differences?..
Thanks
Ravi

date_diff()
example:
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');

$date1 = date("Y/m/d");
$comp_date = "2010/12/1";
if($date1 <= $comp_date)//-------- if for date is less than limit
{
}
else if($date1 > $comp_date)//---------ElseIf for date is greator than limit
{
}//----End else if($date1 > $comp_date)
}//--------- End Main if
You can try this method too...

Related

How to calculate the difference of two dates [duplicate]

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 :)

Difference between two time values in php [duplicate]

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');

Collect the number of days between two dates [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find number of days between two dates using php
Collect the number of days between two dates, for example
02/11/2012
And between
02/12/2012
The result is the number of days = 1 day
try this
function dateDiff ($d1, $d2) {
return round(abs(strtotime($d1)-strtotime($d2))/86400);
}
The function uses the PHP ABS() absolute value to always return a postive number as the number of days between the two dates.
PHP >= 5.3 you can use DateTime::Diff:
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
echo (strtotime('02/12/2012') - strtotime('02/11/2012')) / (24*60*60);
This line converts both dates to unix timestamps, subtracts them and divides this by the amount of seconds in a day.
You can convert your date to a timestamp with the strtotime() function:
$date1 = strtotime("02/11/2012");
$date2 = strtotime("02/12/2012");
$difference = $date1 - $date2;
Then you have the difference in seconds which is a new timestamp, so you can convert this to days with the date() function:
$days = date("d", $difference);

PHP Difference in months between two dates? [duplicate]

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');
?>

PHP difference date to day [duplicate]

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";

Categories