Php obtains the start time, end time, and end time of each week and month between a specified date. php date_php tutorial

Source: Internet
Author: User
Php obtains the start and end time of each week and month between the specified date, and the php date. Php obtains the start time and end time of each week and month between a specified date. The php date calculates the start time and end time of each week between the specified two dates, and php obtains the start time, end time, and end time of each week and month between a specified date.

Calculate the start time and end time of each week between the specified two dates, and the start time and end time of each month.

Log formatting class Date. class. php

The code is as follows:


<? Php
Class Datefmt {
Function _ construct (){}
/**
* Obtain the start time and end time of the week based on the specified date.
*/
Public function get_weekinfo_by_date ($ date ){
$ Idx = strftime ("% u", strtotime ($ date ));
$ Mon_idx = $ idx-1;
$ Sun_idx = $ idx-7;
Return array (
'Week _ start_day '=> strftime (' % Y-% m-% d', strtotime ($ date)-$ mon_idx x * 86400 ),
'Week _ end_day '=> strftime (' % Y-% m-% d', strtotime ($ date)-$ sun_idx * 86400 ),
);
}
/**
* Obtain the start time and end time of the month based on the specified date.
*/
Public function get_monthinfo_by_date ($ date ){
$ Ret = array ();
$ Timestamp = strtotime ($ date );
$ Mdays = date ('t', $ timestamp );
Return array (
'Month _ start_day '=> date ('Y M-1', $ timestamp ),
'Month _ end_day '=> date ('Y-m-'. $ mdays, $ timestamp)
);
}
/**
* Obtain the week between the specified date
*/
Public function get_weeks ($ sdate, $ edate ){
$ Range_arr = array ();
// Check the validity of the date
$ This-> check_date (array ($ sdate, $ edate ));
// Calculate the start time of each week
Do {
$ Weekinfo = $ this-> get_weekinfo_by_date ($ sdate );
$ End_day = $ weekinfo ['week _ end_day '];
$ Start = $ this-> substr_date ($ weekinfo ['week _ start_day ']);
$ End = $ this-> substr_date ($ weekinfo ['week _ end_day ']);
$ Range = "{$ start }~ {$ End }";
$ Range_arr [] = $ range;
$ Sdate = date ('Y-m-D', strtotime ($ sdate) + 7*86400 );
} While ($ end_day <$ edate );
Return $ range_arr;
}
/**
* Obtain the months between the specified date.
*/
Public function get_months ($ sdate, $ edate ){
$ Range_arr = array ();
Do {
$ Monthinfo = $ this-> get_monthinfo_by_date ($ sdate );
$ End_day = $ monthinfo ['month _ end_day '];
$ Start = $ this-> substr_date ($ monthinfo ['month _ start_day ']);
$ End = $ this-> substr_date ($ monthinfo ['month _ end_day ']);
$ Range = "{$ start }~ {$ End }";
$ Range_arr [] = $ range;
$ Sdate = date ('Y-m-D', strtotime ($ sdate. '+ 1 month '));
} While ($ end_day <$ edate );
Return $ range_arr;
}
/**
* Truncate the month and day in the date
* @ Param string $ date
* @ Return string $ date
*/
Public function substr_date ($ date ){
If (! $ Date) return FALSE;
Return date ('M-D', strtotime ($ date ));
}
/**
* Check the validity of the date YYYY-mm-dd
* @ Param array $ date_arr
* @ Return boolean
*/
Public function check_date ($ date_arr ){
$ Invalid_date_arr = array ();
Foreach ($ date_arr as $ row ){
$ Timestamp = strtotime ($ row );
$ Standard = date ('Y-m-D', $ timestamp );
If ($ standard! = $ Row) $ invalid_date_arr [] = $ row;
}
If (! Empty ($ invalid_date_arr )){
Die ("invalid date->". print_r ($ invalid_date_arr, TRUE ));
}
}
}

Usage of class files

The code is as follows:


<? Php
Require_once "Datefmt. class. php ";
$ Datefmt = new Datefmt ();
// Output the weeks between October 1 and October 30
Var_dump ($ datefmt-> get_weeks ('2017-11-05 ', '2017-11-29 '));
/* The result is
Array (4 ){
[0] =>
String (13) "11-03 ~ 11-09"
[1] =>
String (13) "11-10 ~ 11-16"
[2] =>
String (13) "11-17 ~ 11-23"
[3] =>
String (13) "11-24 ~ 11-30"
}
*/
// Output the values of February 1 and November 30
Var_dump ($ datefmt-> get_months ('2017-02-03 ', '2017-11-29 '));
/* The result is
Array (10 ){
[0] =>
String (13) "02-01 ~ 02-28"
[1] =>
String (13) "03-01 ~ 03-31"
[2] =>
String (13) "04-01 ~ 04-30"
[3] =>
String (13) "05-01 ~ 05-31"
[4] =>
String (13) "06-01 ~ 06-30"
[5] =>
String (13) "07-01 ~ 07-31"
[6] =>
String (13) "08-01 ~ 08-31"
[7] =>
String (13) "09-01 ~ 09-30"
[8] =>
String (13) "10-01 ~ 10-31"
[9] =>
String (13) "11-01 ~ 11-30"
}
*/

Week calculates the start time and end time of each week between the two dates based on the specified start time and end time, and...

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.