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...
Related
I am trying to calculate the difference in months between two dates, but in a more specific way.
For example, I have two dates: 2017-11-01 and 2018-01-31
What I need as a result is 3 months. Meaning, there are 3 full billing months between these two dates.
Here is how it's supposed to work:
Month 1: 2017-11-01 until 2017-11-30
Month 2: 2017-12-01 until 2017-12-31
Month 3: 2018-01-01 until 2018-01-31
I have tried the diff method in the DateTime class, it produces something that doesn't help much. Here is an example
<?php
$date1 = DateTime::createFromFormat('Y-m-d H:i:s', '2017-11-01 00:00:00');
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2018-01-31 00:00:00');
$diff = $date1->diff($date2);
print_r($diff)
result:
DateInterval Object
(
[y] => 0
[m] => 2
[d] => 30
[h] => 0
[i] => 0
[s] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 91
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
It shows 2 months and 30 days.
However, in a slightly different scenario
$date1 = DateTime::createFromFormat('Y-m-d H:i:s', '2017-11-01 00:00:00');
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2018-01-30 00:00:00');
The diff between these two dates should show 2 months and 30 days, not 3 months.
Any help or ideas would be greatly appreciated.
Add one day to your ending date before doing the comparison. If the original ending date was the last day of the month, then the new ending date will roll over to the next month and you'll get the correct number of "full" months in the diff object. If the original was any day but the last day of the month, it won't change the result.
$start = '2017-11-01';
$end = '2018-01-31';
$date1 = DateTime::createFromFormat('Y-m-d', $start);
$date2 = DateTime::createFromFormat('Y-m-d', $end)->add(new DateInterval('P1D'));
echo $date1->diff($date2)->m, "\n";
Seems you lost one day during your calculation. Cause you need interval in months including first/last day - then you should add this day to interval.
So, the solution in this case will be:
$date1 = DateTime::createFromFormat('Y-m-d H:i:s', '2017-11-01 00:00:00');
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2018-01-31 23:59:59');
$diff = $date1->diff($date2);
or:
$date1 = DateTime::createFromFormat('Y-m-d H:i:s', '2017-10-31 00:00:00');
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2018-01-31 00:00:00');
$diff = $date1->diff($date2);
or even:
$date1 = DateTime::createFromFormat('Y-m-d H:i:s', '2017-11-01 00:00:00');
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2018-02-01 00:00:00');
$diff = $date1->diff($date2);
Using Carbon:
Carbon::parse('2017-10-31')->diffInMonths(Carbon::now());
I want to find difference between these two date with days,hours and minutes.
$date1 = "27-09-2014 05:00 AM";
$date2 = "29-09-2014 03:00 PM";
From PHP Version > 5 below new date/time functions added to get difference:
$datetime1 = new DateTime("2010-06-20");
$datetime2 = new DateTime("2011-06-22");
$difference = $datetime1->diff($datetime2);
echo 'Difference: '.$difference->y.' years, '
.$difference->m.' months, '
.$difference->d.' days';
print_r($difference);
Result as below:
Difference: 1 years, 0 months, 2 days
DateInterval Object
(
[y] => 1
[m] => 0
[d] => 2
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 367
)
I'm not sure whether I get your question right, but shouldn't the following work?
$datetime1 = new DateTime('27-09-2014');
$datetime2 = new DateTime('29-09-2014');
$datetime1->setTime(05, 00);
$datetime2->setTime(15, 00);
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%a Day and %h hours'
DateTime::diff
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;
I'm trying to get the time difference in milliseconds.
$_SESSION['startTime'] = time();
$to_time = time();
//I call the code from here after a delay, say 4 seconds
$from_time = $_SESSION['startTime'];
$d1 = new DateTime($from_time);
$d2 = new DateTime($to_time);
print_r( $d1->diff($d2));
I print the result after 4 seconds and the result is somewhat like this:
DateInterval Object
(
[y] => 4 //---- Problem, this value should be +
[m] => 0 // |
[d] => 0 // |
[h] => 0 // |
[i] => 0 // |
[s] => 0 //<-here-----------------------------+
[invert] => 1
[days] => 1461
)
[s] should have been 4. why the 4 is in the year section?
What am I doing wrong?
UPDATE - Solved
$to_time = (microtime(true));
$from_time = ( $_SESSION['startTime']);
$diff = $to_time - $from_time;
print $diff;
Prints
3.xxxxxx
You must specify the formatting. You're sending in a unix timestamp into DateTime, therefor:
$d1 = new DateTime($from_time);
$d2 = new DateTime($to_time);
Becomes
$d1 = new DateTime('#'.$from_time);
$d2 = new DateTime('#'.$to_time);
The # symbol tells DateTime that I'm using a Unix Timestamp.
The constructor for DateTime accepts a string as a parameter not a timestamp, which is why you are seeing the "strange behaviour".
You need to expressly set the timestamp after insantiating a DateTime object:-
$from_time = $_SESSION['startTime'];
$d1 = new DateTime();
$d1->setTimestamp($from_time);
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