PHP gets the start and end times of each week and month between specified dates, PHP date _php tutorial

Source: Internet
Author: User

PHP gets the start and end time of each week and month between the specified date, PHP date


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

Log Formatting class Date.class.php

Copy CodeThe code is as follows:
<?php
Class datefmt{
function __construct () {}
/**
* Gets the starting and ending 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)
);
}
/**
* Gets 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 for 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 interception 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

The

copy Code code is as follows:
<?php
require_once "Datefmt.class.php";
$datefmt = new Datefmt ();
Outputs
Var_dump ($datefmt->get_weeks (' 2014-11-05 ', ' 2014-11-29 ') for each week between November 1 and 30th;
//result is
Array (4) {
[0 ]=>
String (11-03 ~ 11-09 "
[1]=>
String] 11-10 ~ 11-16"
[2]=>
String (13) "11-17 ~ 11- "
[3]=>
String (11-24 ~ 11-30"
}
*/
/output February 1 and November 30
Var_dump ($datefmt->get_months (' 2014-02-03 ', ' 2014-11-29 ');
/* result is
Array (ten) {
[0]=>
String (02-01 ~ 02-28 "
[1]=>
string ()" 03-01 ~ 03-31 "
[2] =>
String (04-01 ~ 04-30 "
[3]=>
string" 05-01 ~ 05-31 "
[4]=>
String (13)" 06-01 ~ 06-3 0 "
[5]=>
String (07-01 ~ 07-31"
[6]=>
string "08-01 ~ 08-31"
[7]=>
String (13) "09 -01 ~ 09-30 "
[8]=>
String (" 10-01 ~ 10-31 "
[9]=>
string" 11-01 ~ 11-30 "
}
*/

http://www.bkjia.com/PHPjc/917042.html www.bkjia.com true http://www.bkjia.com/PHPjc/917042.html techarticle PHP Gets the start and end time of each week and month between the specified dates, and the PHP date calculates the starting and ending times of each week between the two dates based on the specified two dates, as well as ...

  • 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.