Hướng dẫn dùng timedelta months trong PHP

Possible Duplicate:
How to calculate the difference between two dates using PHP?
Date Difference in php?

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged php datediff or ask your own question.
  • How can I get the number of months between two dates in php?
  • How can I get the difference between two dates in php?
  • How get number of days in a month in php?
  • How do you find the difference between two dates?

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?

asked May 3, 2012 at 8:26

3

A more elegant solution is to use DateTime and DateInterval.


Chintan

1,1641 gold badge7 silver badges22 bronze badges

answered May 3, 2012 at 8:28

DevatorDevator

3,4164 gold badges32 silver badges52 bronze badges

8

Not the answer you're looking for? Browse other questions tagged php datediff or ask your own question.

When you calculate the difference between two PHP DateTime objects, it returns a DateInterval object. From this DateInterval object, you can access the year and month to calculate the total months between two dates like so:

$start = new DateTime['2020-01-01 00:00:00'];
$end = new DateTime['2021-03-15 00:00:00'];
$diff = $start->diff[$end];

$yearsInMonths = $diff->format['%r%y'] * 12;
$months = $diff->format['%r%m'];
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14

Using the DateInterval::format[] method with %y and %m format specifiers, you get the year and month respectively. The %r format specifier, simply adds the "-" sign when the difference is negative. To test this you switch the $end and $start dates to see a negative time period:

$start = new DateTime['2020-01-01 00:00:00'];
$end = new DateTime['2021-03-15 00:00:00'];
$diff = $end->diff[$start];

$yearsInMonths = $diff->format['%r%y'] * 12;
$months = $diff->format['%r%m'];
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14

If you do not wish to include the negative sign for negative time periods, you can do either of the following:

  • Simply remove the %r format specifier when retrieving the year and month, or;
  • Pass true as the second argument to the DateTime::diff[] method to always return an absolute / positive interval.

Instead of using the DateInterval::format[] method, you can also directly access the year and month values using the y and m properties on the DateInterval object. Similarly, to check for a negative time period, you can access the invert property [which equals 1 if the interval represents a negative time period and 0 otherwise].

Hope you found this post useful. It was published 02 Aug, 2021. Please show your love and support by sharing this post.

In this article, we will see how to calculate the difference between 2 dates in PHP, along with understanding its implementation through the examples. Given two dates ie., start_date and end_date & we need to find the difference between the two dates.

Consider the below example:

Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.

Method 1: Using date_diff[] Function

This function is used to find the difference between two dates. This function will return a DateInterval object on the success and returns FALSE on failure.

Example: This example illustrates the use of the date_diff[] function to calculate the difference between the 2 dates.

PHP

Output:

+2 years 3 months

Method 2: To use the date-time mathematical formula to find the difference between two dates. It returns the years, months, days, hours, minutes, seconds between two specified dates.

Example: In this example, we will be using the date-time mathematical formula to calculate the difference between the dates that will be returned in years, months, days, hours, minutes, seconds.

PHP

Output:

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds

Method 3: This method is used to get the total number of days between two specified dates.

PHP

Output:

Difference between two dates: 103

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


How can I get the number of months between two dates in php?

php $sdate = "1981-11-04"; $edate = "2013-09-04"; $date_diff = abs[strtotime[$edate] - strtotime[$sdate]]; $years = floor[$date_diff / [365*60*60*24]]; $months = floor[[$date_diff - $years * 365*60*60*24] / [30*60*60*24]]; $days = floor[[$date_diff - $years * 365*60*60*24 - $months*30*60*60*24]/ [60*60*24]]; printf["%d ...

How can I get the difference between two dates in php?

PHP date_diff[] Function $date2=date_create["2013-12-12"]; $diff=date_diff[$date1,$date2];

How get number of days in a month in php?

The cal_days_in_month[] function returns the number of days in a month for a specified year and calendar.

How do you find the difference between two dates?

Use the DATEDIF function when you want to calculate the difference between two dates. First put a start date in a cell, and an end date in another..

Use DATEDIF to find the total years. ... .

Use DATEDIF again with “ym” to find months. ... .

Use a different formula to find days..

Chủ Đề