Use PHP to calculate the year, month, week, and number of days between two dates:
Copy codeThe Code is as follows:
<? Php
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 '));
?>
Running result:
Array ([extends] => 75 [monthly] => 2 [weekly] => 2)
Php queries the number of weeks of a day and the start date of the corresponding week
Copy codeThe Code is as follows:
/**
* @ File
* @ Version 1.1
* @ Author QQ83989686
* @ Date 2012-8-7 last modification time
* @ Brief
*/
// Obtain the number of weeks and the start and end time of a week.
Private function getWeekStartEndDay ($ day)
{
$ G = strftime ("% u", strtotime ($ day ));
Return array ('Week _ num' => strftime ("% V", strtotime ($ day )), 'Week _ start_day '=> strftime (' % Y-% m-% d', strtotime ($ day)-($ g-1) * 86400 ), 'Week _ start_day_cn '=> strftime (' % Y % m % dday', strtotime ($ day)-($ g-1) * 86400 ), & apos; week _ end_day & apos; = & apos; strftime ('% Y-% m-% d', strtotime ($ day) + (7-$ g) * 86400 ), 'Week _ end_day_cn '=> strftime (' % Y % m % dday', strtotime ($ day) + (7-$ g) * 86400 ));
}