Php calculates the number of days between two dates _ PHP Tutorial

Source: Internet
Author: User
Php calculates the number of days between two dates. This article will introduce to you in detail the example of the number of days between two dates calculated by php. for your reference, we generally use strtotime to convert the date and then calculate it, this article will introduce to you in detail the example of the number of days between two dates calculated by php. for your reference, we generally use strtotime to convert the date and then calculate it, this can be accurate to minutes and seconds.


Example 1

Convert the date directly

The code is as follows:

Function daysbetweendates ($ date1, $ date2 ){
$ Date1 = strtotime ($ date1 );
$ Date2 = strtotime ($ date2 );
$ Days = ceil (abs ($ date1-$ Date 2)/86400 );
Return $ days;
}

Example 2

The code is as follows:

Functionmaketime ($ date)
{
List ($ year, $ month, $ day) = explode ('-', $ date );
Returnmktime (0, 0, 0, $ month, $ day, $ year );
}
$ Date1 = '2017-01-08 ';
$ Date2 = '2017-03-01 ';
$ D = (maketime ($ date2)-maketime ($ date1)/(24x3600 );
Echo 'difference $ d Day ';

?>


Example 3

PHP calculates the year, month, week, and number of days between two dates.

The code is as follows:
Function format ($ a, $ B ){
// Check the size of two dates. the default value is "first small" and "later". if the former value is greater than the latter value, the positions are exchanged to ensure the former value is greater than the former value.
If (strtotime ($ a)> strtotime ($ B) list ($ a, $ B) = array ($ B, $ );
$ Start = strtotime ($ );
$ Stop = strtotime ($ B );
$ Extend = ($ stop-$ start)/86400;
$ Result ['extends '] = $ extend;
If ($ extend <7) {// returns the number of days if it is less than 7 days
$ Result ['Daily '] = $ extend;
} Elseif ($ extend <= 31) {// The number of weeks is returned if it is less than 28 days. because the leap year meets
If ($ stop = strtotime ($ a. '+ 1 month ')){
$ Result ['monthly '] = 1;
} Else {
$ W = floor ($ extend/7 );
$ D = ($ stop-strtotime ($ a. '+'. $ w. 'Week ')/86400;
$ Result ['weekly '] = $ w;
$ Result ['Daily '] = $ d;
}
} Else {
$ Y = floor ($ extend/365 );
If ($ y> = 1) {// if it is more than one year
$ Start = strtotime ($ a. '+'. $ y. 'year ');
$ A = date ('Y-m-D', $ start );
// Determine whether it has been a year. if not, subtract it.
If ($ start> $ stop ){
$ A = date ('Y-m-D', strtotime ($ a. '-1 month '));
$ M = 11;
$ Y --;
}
$ Extend = ($ stop-strtotime ($ a)/86400;
}
If (isset ($ m )){
$ W = floor ($ extend/7 );
$ D = $ extend-$ w * 7;
} Else {
$ M = isset ($ m )? $ M: round ($ extend/30 );
$ Stop> = strtotime ($ a. '+'. $ m. 'month ')? $ M: $ m --;
If ($ stop> = strtotime ($ a. '+'. $ m. 'month ')){
$ D = $ w = ($ stop-strtotime ($ a. '+'. $ m. 'month')/86400;
$ W = floor ($ w/7 );
$ D = $ d-$ w * 7;
}
}
$ Result ['early '] = $ y;
$ Result ['monthly '] = $ m;
$ Result ['weekly '] = $ w;
$ Result ['Daily '] = isset ($ d )? $ D: null;
}
Return array_filter ($ result );
}

Print_r (format ('2017-10-1 ', '2017-12-15 '));
?>

Worker calculates the number of days between two dates. for your reference, we generally use strtotime to convert the date and then calculate it ,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.