PHP Date aspects

Source: Internet
Author: User
Date_default_timezone_set (' PRC ');
/**
* To find a date after a certain number of days from a certain date,
* Exclude Saturday Sundays and holidays
* @param $start Start date
* @param $offset days Elapsed
* @param $exception exceptions for holidays
* @param $allow allowed date (reserved parameters)
* @return
* Examples: Input (2010-06-25, 5, '), get 2010-07-02
*/
function Getendday ($start = ' Now ', $offset =0, $exception = ', $allow = ') {
The results of the Saturday Sundays and holidays are calculated first
$starttime = Strtotime ($start);
$endtime = $starttime + $offset * 24 * 3600;
$end = Date (' y-m-d ', $endtime);
Then calculate the offsets caused by Sunday Saturday
$weekday = Date (' N ', $starttime);//Get Weekday value: 1-7
$remain = $offset% 7;
$newoffset = 2 * ($offset-$remain)/7;//a recalculation of two days per week
if ($remain > 0) {//Spoke rounding
$tmp = $weekday + $remain;
if ($tmp >= 7) {
$newoffset + = 2;
}else if ($tmp = = 6) {
$newoffset + = 1;
}
Consider the current situation for the Saturday Sunday
if ($weekday = = 6) {
$newoffset-= 1;
}else if ($weekday = = 7) {
$newoffset-= 2;
}
}
To calculate the offsets caused by the holidays
if (Is_array ($exception)) {//Multiple holidays
foreach ($exception as $day) {
$tmp _time = Strtotime ($day);
if ($tmp _time> $starttime && $tmp _time<= $endtime) {//within the range (A, b]
$weekday _t = Date (' N ', $tmp _time);
if ($weekday _t <= 5) {//Prevent holidays and weekends from repeating
$newoffset + = 1;
}
}
}
}else{//Individual Holidays
if ($exception! = ") {
$tmp _time = Strtotime ($exception);
if ($tmp _time> $starttime && $tmp _time<= $endtime) {
$weekday _t = Date (' N ', $tmp _time);
if ($weekday _t <= 5) {
$newoffset + = 1;
}
}
}

}
Recursive equivalent operation based on offset days
if ($newoffset > 0) {
#echo "[{$start}, {$offset}] = [{$end}, {$newoffset}]". " <br/>\n ";
Return Getendday ($end, $newoffset, $exception, $allow);
}else{
return $end;
}
}
/**
* Methods of violent circulation
*/
function getEndDay2 ($start = ' Now ', $offset =0, $exception = ', $allow = ') {
$starttime = Strtotime ($start);
$tmptime = $starttime + 24*3600;

while ($offset > 0) {
$weekday = Date (' N ', $tmptime);
$tmpday = Date (' y-m-d ', $tmptime);
$BFD = false;//whether holidays
if (Is_array ($exception)) {
$BFD = In_array ($tmpday, $exception);
}else{
$BFD = ($exception = = $tmpday);
}
if ($weekday <=5 &&! $bfd) {//not weekends and holidays
$offset--;
#echo "tmpday={$tmpday}". <br/> ";
}
$tmptime + = 24*3600;
}

return $tmpday;
}
$exception = Array (
' 2010-01-01 ', ' 2010-01-02 ', ' 2010-01-03 ',
' 2010-04-03 ', ' 2010-04-04 ', ' 2010-04-05 ',
' 2010-05-01 ', ' 2010-05-02 ', ' 2010-05-03 ',
' 2010-06-14 ', ' 2010-06-15 ', ' 2010-06-16 ',
' 2010-09-22 ', ' 2010-09-23 ', ' 2010-09-24 ',
' 2010-10-01 ', ' 2010-10-02 ', ' 2010-10-03 ', ' 2010-10-04 ',
' 2010-10-05 ', ' 2010-10-06 ', ' 2010-10-07 ',

);
echo getendday (' 2010-08-27 ', 3, ');
echo getendday (' 2010-06-25 ', 15, ' 2010-07-07 ');
$t 1 = microtime ();
echo getendday (' 2010-05-12 ', $exception). " <br/> ";
$t 2 = Microtime (); echo "Use". ($t 2-$t 1). " s <br/> ";
echo getEndDay2 (' 2010-05-12 ', $exception). " <br/> ";
$t 3 = Microtime (); echo "Use". ($t 3-$t 2). " s <br/> ";
  • 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.