PHP gets the start and end times of each week and month between the specified dates _php instance

Source: Internet
Author: User

Calculates the start and end times of each week between these two dates, and the start and end times of each month, based on the specified two dates

Log Format Class Date.class.php

Copy Code code as follows:

<?php
Class datefmt{
function __construct () {}
/**
* Get the start and end times 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 * 86400),
' Week_end_day ' => strftime ('%y-%m-%d ', Strtotime ($date)-$sun _idx * 86400),
);
}
/**
* Get the starting and ending times 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)
);
}
/**
* Get each week between the specified dates
*/
Public Function Get_weeks ($sdate, $edate) {
$range _arr = Array ();
Check Date Validity
$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;
}
/**
* Get each month between the specified dates
*/
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;
}
/**
* The month and day of the Intercept 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));
}
}
}

Use of class files

Copy Code code as follows:

<?php
Require_once "Datefmt.class.php";
$DATEFMT = new Datefmt ();
Outputs the weeks between November 1 and 30th
Var_dump ($datefmt->get_weeks (' 2014-11-05 ', ' 2014-11-29 '));
/* Results are
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 February 1 and November 30
Var_dump ($datefmt->get_months (' 2014-02-03 ', ' 2014-11-29 '));
/* Results are
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"
}
*/

Related Article

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.