Collect the number of days between two dates [duplicate] - php

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

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

Find Minimum of two given times in PHP [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
Can somebody help me compare two times and found out which one is less than other. by less I mean which one has older time than other.
Times are retrieved from database :
$time1 = strtotime($row[0]);
$time2 = strtotime($row2[0]);
If you only want to know which date is earlier, you can simply compare them.
Here is an example from the Php manual :
$d1 = new DateTime('1492-01-01');
$d2 = new DateTime('1492-12-31');
var_dump($d1 < $d2);
var_dump($d1 > $d2);
var_dump($d1 == $d2);
?>
Results :
bool(true)
bool(false)
bool(false)
You have to convert your input dates into timestamps, then just subtract one from the other.
$timestampA = 1221672010;
$timestampB = 1221671010;
$diff = $timestampA - $timestampB;
if ( $diff > 0 ) {
// $timestampA date is after $timestampB date
} else if ( $diff < 0 ) {
// $timestampA date is before $timestampB date
} else {
// dates are equal
}
To convert string date into timestamp you can use strtotime function.
I believe that by 2 times you mean 2 dates.
You can always convert them to timestamp and them compare the two numbers.
for that you can use strtotime function
If you mean time() for times data use min function: http://fr.php.net/min
Otherwise, http://php.net/manual/en/datetime.diff.php
$time1 = time();
$time2 = time()-3600;
var_dump(min($time1, $time2));
Or
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days')
you can use the time() function witch returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
or if you have the date-time in string format you can use strtotime() to convert it before comparaison.
The smallest number is the oldest time !
$time = strtotime(min(array($row[0], $row2[0])));
Gives you the earliest time.

Count total month

I have to find total months between two dates in Unix timestamp formats. I want to create a PHP function for that. I've tried this:
get_total_month($startunixdate, $endunixdate) {
$monthdiff = $endunixdate-$startunixdate;
$monthdiff = $monthdiff / 60*60*24*31;
return $monthdiff;
}
Does this function consider leap years as well as month with 30 and 31 separately, or it will just count an approximate month?
Your answer is in this line;
$monthdiff = $monthdiff / 60*60*24*31
This will just count a month based on 31 days. The code divides the seconds by 60 to get the number of minutes, 60 again to get hours, 24 to get number of days, then it uses 31 as the length of a month.
This will result in an approximation of the number of months. For instance, if you take timestamps at the beginning and end of February (start of March), you will usually have a timestamp difference equivalent to 28 days (29 in a leap year). Dividing that by 31 will give you a fraction less than 1, since you are using 31 to represent a full month, when in reality a whole calendar month has passed.
In order to do this, use the DateTime class.
function get_total_month($start, $end) {
// Create DateTime objects
$dt1 = new DateTime($start);
$dt2 = new DateTime($end);
// Get DateInterval object representing difference between the two
$diff = $dt1->diff($dt2); // OR: $diff = date_diff($dt1, $dt2);
// Print the "months" out
echo $diff->format("Difference: %R%m months"); // OR access $diff->m manually
U can use PHP 5.3 datetime method.
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Reference: PHP DateTime

Difference between dates [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Difference between dates
How to calculate the date difference between 2 dates using php
So, I have two dates. For instance, 2010-09-24 and 2010-09-25. I want to check if between those two dates the difference is 1 day.
I don't need to get a value of 86400 which means one day in seconds.
Don't forget that it can be 28, 28, 29, 30, 31 days in month.
Thanks.
What I have right now, but it doesn't work when there is difference between months:
$strto = strtotime($date);
$d1 = date('d', $strto);
$d2 = date('d', time());
echo $d2- $d1;
You can use strtotime to get the number of seconds in between
echo abs(strtotime('2010-09-24') - strtotime('2010-09-25'));
Don't use the day value - (eg date('d', ...)) - leave it as an integer (the result of strtotime()). Then subtract those dates, and then get the floor(difference / 86400).
Like so:
$dt = strtotime($date);
echo floor(abs(time() - $dt) / 86400);
You can do this nicely with the DateTime class if you have PHP 5.3:
<?php
$datetime1 = new DateTime('2010-09-25');
$datetime2 = new DateTime('2010-09-26');
$interval = $datetime1->diff($datetime2);
$intervaldays = (int) $interval->format('%R%d'); // %R signs the result +/-
This is probably less efficient than using strtotime methods, but it is very readable.
Why are you using date('d'... which returns the day of the month?
strtotime will create a UNIX-timestamp which is exactly what time() returns, so abs(time() - strtotime($date)) should already do the job. This way you don't have to worry how many days a month has as you're only working with timestamps.
This will get you the number of (complete) days:
floor( abs(time() - strtotime($date)) / 86400 )

PHP: get number of days between two dates in format YYYY-MM-DD [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to find number of days between two dates using php
Is there a quick and easy way to calculate the difference in days between two date strings in this format (YYYY-MM-DD) with PHP (not MySQL)?
$date1 = new DateTime("2010-07-06"); //inclusive
$date2 = new DateTime("2010-07-09"); //exclusive
$diff = $date2->diff($date1);
echo $diff->format("%a"); //3
(PHP 5.3 and higher only)
The only solution I see for PHP < 5.2 is to loop:
strtotime("-1 days");
strtotime("-2 days");
...
strtotime("-n days");
until we get to the unix timestamp of the first date. That's conceptually, you can do it in a much more efficient way, by first guessing the number of days with the timestamp difference of the two days and then testing the neighborhood.
Why dividing by 86400 doesn't work
date_default_timezone_set("Europe/Lisbon");
$date1 = strtotime("2010-03-28");
$date2 = strtotime("2010-03-29");
echo ($date2-$date1)/86400; //gives 0.95833333333333
$date1 = strtotime("2010-10-31");
$date2 = strtotime("2010-11-01");
echo ($date2-$date1)/86400; //gives 1.0416666666667
As Gordon correctly has pointed out, dividing by 86400 would be a valid solution for this problem if the timezone was set to 'UTC' before – just don't forget to restore it to the previous value after.
You can use this function to get the number of days between two date("Y-m-d H:i:s"):
function dateDiff($dateStart, $dateEnd)
{
$start = strtotime($dateStart);
$end = strtotime($dateEnd);
$days = $end - $start;
$days = ceil($days/86400);
return $days;
}
Copied from the Duplicate I've linked below the question.
The following SO questions might be of some help:
How to calculate the date difference between 2 dates using php
Dates difference with php
Calculate the difference between date/times in PHP
Date Difference in php?
Getting the difference between two time/dates using php?
More >>

Categories