This question already has answers here:
Date Difference in php on days? [duplicate]
(3 answers)
Closed 9 years ago.
i want to minus two dates in php
for example:
$date1 = 08/16/2013;
$date2 = 08/23/2013;
$answer = date2 - date1;
the $answer should be 7, How will i do that?
thank you so much
Start using DateTime class for date/time manipulation :
$date1 = new DateTime('08/16/2013');
$date2 = new DateTime('08/23/2013');
$diff = $date1->diff($date2);
print_r($diff); // or $diff->days
Output :
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 7
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 7
)
Read more for DateTime:diff().
Please note that various strtotime() examples are not correct in date/time difference calculation. The simplest example is difference between 2013-03-31 21:00 and 2013-03-30 21:00. Which for naked eye is exact 1 day difference, but if you do subtract this 2 dates, you will get 82800 seconds which is 0.95833333333333 days. This is because of the time change from winter to summer time. DateTime handles leap years and time-zones properly.
Try this -
<?php
$date1 = strtotime('08/16/2013');
$date2 = strtotime('08/23/2013');
echo $hourDiff=round(abs($date2 - $date1) / (60*60*24),0);
?>
You can get with strtotime and minus dates
$diff = abs(strtotime('08/16/2013') - strtotime('08/23/2013'));
echo $min = floor($diff / (60*60*24)); // 7
$date1 = '08/16/2013';
$date2 = '08/23/2013';
$days = (strtotime($date2) - strtotime($date1)) / (60 * 60 * 24);
print $days;
Related
I need to work out the number of months between a date like this:
$inputDate = '09/08/2016';
that is entered in the MM/DD/YYYY format, and the current date, e.g.:
$today = date("m/d/Y");
I've been looking at the date_diff but can't seem to get the syntax right here and appreciate any help here.
You can use DateTime and diff
$datetime1 = new DateTime();
$datetime2 = new DateTime('09/08/2016');
$interval = $datetime1->diff($datetime2);
$months = $interval->format('%m months'); # you can also use %a days %h hours %i minutes %s seconds
echo $months;
# 8 months
# optimally you can use:
# echo $datetime1->diff($datetime2)->y*12;
# or
# echo $interval->m
Update based-on comments:
$datetime1 = new DateTime();
$datetime2 = new DateTime('09/08/2015');
$interval = $datetime1->diff($datetime2);
echo (($interval->format('%y') * 12) + $interval->format('%m')) . " full months difference";
Note:
A DateInterval Object looks like:
DateInterval Object
(
[y] => 3
[m] => 5
[d] => 15
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 1264
)
That's why we've to multiply the number of years * 12 and add the months in order to get the difference in months. Strange, but this is how it works...
i am implementing one application in which i have to display a time difference in total hour from end date to start date. suppose i have both start date and end date in timestamp.so how i will get the difference of date in total hour ?
now i am using the below code
$intervalo = date_diff(date_create(), date_create($end));
pr($intervalo);
and the output is like
DateInterval Object
(
[y] => 0
[m] => 1
[d] => 4
[h] => 18
[i] => 41
[s] => 2
[invert] => 0
[days] => 34
)
the above code show me the total hour baut the end date is greater then 1month it shows 1m in array . but i only want total number of hour only
can any one help me ?
thanks in advance
Don't use date_diff. Convert the dates to timestamps (which represent seconds), subtract the two, and divide by 3600.
$x = date_create();
$y = date_create($end)
$hours = ($y->getTimestamp() - $x->getTimestamp()) / 3600;
If you want hours, minutes, and seconds:
$x = date_create();
$y = date_create($end)
$diff = ($y->getTimestamp() - $x->getTimestamp());
$seconds = $diff % 60;
$diff = (int)($diff / 60);
$minutes = $diff % 60;
$diff = (int)($diff / 60);
$hours = $diff;
I have a date that returns in a string as 2012-03-19 05:00:32, its not coming from the database
I can use below to search for the last 30 days
$date = '2012-03-19 05:00:32';
if (strtotime($date) >= strtotime('-7 days')) {
// do something
}
Problem is if today is the 19th March, i was to search from the 11th to the 18th for the last 7 days and that seems to search for the last 7 days by calculating 24 hours * 7 by my searches need to start from 00:00:01 each day.
My plan is to break the date down into Year, Month and Day then check if year = 12, then check if month = 3, then check if date between 11 and 18.
Im just wondering if there is a more efficient way to do this or if im on the right track.
I also have the same issue with running a search on all info from this month and also want to search for all info this week starting on Monday.
So this is just asking if my method is sound or if there is a more efficient method.
$mytime = new DateTime('2012-03-19 05:00:32');
$mydate = new DateTime($mytime->format('Y-m-d')); //keep date only, exclude the time component
$now=new DateTime; //includes hours, minutes, seconds
$today=new DateTime($now->format('Y-m-d')); //time set to 0:00
$interval = $mydate->diff($today);
if($interval->format('d') <=7) { //assuming that $mydate isn't in the past
//do something
}
I'd suggest to use DateTime class ...
<?php
$d1=new DateTime("2012-07-08 11:14:15.638276");
$d2=new DateTime("2012-07-08 11:14:15.889342");
$diff=$d2->diff($d1);
print_r( $diff ) ;
/* returns:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 0
)
*/
?>
I need your help in how to subtract the last_modified and the final_manuscript_date in the following array:
Array (
[chapters_id] => 10736
[last_modified] => 2010-12-21 15:01:55
[steps_id] => 3
[sub_step_id] => 0
[steps_position] => 1
[final_manuscript_date] => 2010-09-27
)
So I can in this case get a value of N days between the dates 2010-12-21 and 2010-09-27?
Can't you simply do:
$diff = strtotime($arr["final_manuscript_date"]) - strtotime($arr["last_modified"]);
$days = $diff / 84600; // to get # of days, you can round them off or use ceil/floor
If you have 5.3+:
$date1 = new DateTime("2010-09-27");
$date2 = new DateTime("2010-12-21");
$interval = $date1->diff($date2);
echo $interval->d //returns days.
Have you checked strtotime?
http://php.net/manual/en/function.strtotime.php
I have two timestamp input one is the current time and another one is future
i.e.
Future time: 2010-8-17 23:00
Present time: 2010-8-15 11:00
I want to setup notification system which will display the time intervals between the above dates. i.e.
15 minutes before
30 minutes before
1 hour before
2 hour before
3 hour before
....
....
....
1 day before
I am not sure how to achieve this task in php, wondering if any one here can suggest me how to achieve this task
How about using the DateTime class.
<?php
$datetime1 = new DateTime('2010-8-15 11:00');
$datetime2 = new DateTime('2010-8-17 23:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%d days, %H hours');
?>
Output:
+2 days, 12 hours
$t1 = getdate($current_date);
$t2 = getdate($future_date);
return $t2[0]-$t1[0];
This will give you the difference between the two dates, measured in seconds.
Get the difference between the two dates and then use date_parse
// $difference = difference in times
print_r(date_parse($difference));
That will result in something along the lines of
Array (
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] => )
See http://php.net/manual/en/function.date-parse.php for more info
This is what I did when I needed the exact same thing.
You can easily extend it by getting the number of seconds in a week, month, or year.
function getTimeInterval($ts1, $ts2)
{
$interval = (int)$ts2 - (int)$ts1;
if ( $interval < 0 ) {
return false;
} else {
$days = floor($interval / 86400); // seconds in one day
$interval = $interval % 86400;
$hours = floor($interval / 3600);
$interval = $interval % 3600;
$minutes = floor($interval / 60);
$interval = $interval % 60;
$seconds = $interval;
}
return array(
'days' => $days,
'hours' => $hours,
'minutes' => $minutes,
'seconds' => $seconds
);
}