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.
Related
I have two timestamps created against dates 02/09/2014 11:30pm and 03/09/2014 12:00am.
There is only 30 minutes difference between these timestamps but as date has changed from O2 October to 03 October, it should be calculated as a day.
My code is
$current_time_zone = isset($_COOKIE['IANA_timezone_key']) ? $_COOKIE['IANA_timezone_key'] : "";
$d1 = new DateTime(date('Y-m-d'), timezone_open($current_time_zone));
$d2 = new DateTime(date('Y-m-d'), timezone_open($current_time_zone));
$d1->setTimestamp($row["transitions_date"]); // $row["transitions_date"] has timestamp value
$d2->setTimestamp($curr_transition_in_date); // $curr_transition_in_date has timestamp value
$diff = date_diff($d1, $d2);
$day_difference = $diff->days;
Any help will be highly appreciated.
You could not expect your desired day difference from the returning object of date_diff() as it is based on the actual time difference. The easiest way would be to adjust it yourself.
$d1 = new DateTime(date('Y-m-d'));
$d2 = new DateTime(date('Y-m-d'));
$d1->setTimestamp($row["transitions_date"]);
$d2->setTimestamp($curr_transition_in_date);
$diff = date_diff($d1, $d2);
$day_difference = $diff->days;
echo 'Actual output from date_diff: '.$day_difference;
echo '<br>';
if($d2->format('H:i:s') < $d1->format('H:i:s')){
$day_difference++;
}
echo 'Corrected output: '.$day_difference;
Demo 1 for the different day, but same month and year.
Demo 2 for the same day, but different month and year.
Demo 3 for the same output.
Hey i would like to know if there is any script (php) that could check if a specified date three days before today.
say..
$d1 = date("Y-m-d", filemtime($testfile));
$d2 = date("Y-m-d");
now i would like to know how to compare this two dates to check if d1 is atleast 3days ago or before d2
any help would be gladly appreciated.
Why not to use DateTime object.
$d1 = new DateTime(date('Y-m-d',filemtime($testfile));
$d2 = new DateTime(date('Y-m-d'));
$interval = $d1->diff($d2);
$diff = $interval->format('%a');
if($diff>3){
}
else {
}
Assuming you wish to test whether the file was modified more than three days ago:
if (filemtime($testfile) < strtotime('-3 days')) {
// file modification time is more than three days ago
}
Just check it with timestamp:
if (time() - filemtime($testfile) >= 3 * 86400) {
// ...
}
use date("Y-m-d", strtotime("-3 day")); for specific date
you can also use
strtotime(date("Y-m-d", strtotime("-3 day")));
to convert it to integer before comparing a date string
well, stunned to see no one is using mktime() function,
it makes the job simple
for example your input date is :10/10/2012
mktime convert it to unix time stamp
$check_date=mktime(0,0,0,10,**10+3**,2012);
we can perform any operations weather +,-,*,/
use timestamp instead of date,
$d1 = filemtime($testfile);
$now = time();
if ($now - $d1 > 3600*24*3) {
..
}
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);
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 )
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 >>