PHP for a period of time each week, the number of months, encountered special days on the back

Source: Internet
Author: User

November 1, 2016 16:18:19 Tuesday

The main function used is strtotime ()

Strtotime (' +1 Tuesday ', $timestamp) gets the next Tuesday, calculated from the timestamp $timestamp, and if $timestamp is left blank, it is calculated from the start of the day

Strtotime (' +1 month ', $timestamp) get the X number for next month, or start with $timestamp.

  1/** 2 * desc Gets a weekly x execution of all dates 3 * @param string $start start date, 2016-10-17 4 * @param string $end end date, 201 6-10-17 5 * @param int $weekDay 6 * @return Array 7 */8 Public Function getweeklybuydate ($star T, $end, $weekDay) 9 {10//Get the day of the week to execute for example: 2016-01-02 $start = Empty ($start)? Date (' y-m-d '): $st Art $startTime = Strtotime ($start); $startDay = Date (' N ', $startTime); if ($startDay < $weekDay) {$startTime = Strtotime (self:: $WORK _day[$weekDay] [' en '], strtotime ($start)); This week x starts, for example, today (Tuesday) user settings are executed every Thursday, and that Thursday will begin to execute the "else {$startTime = Strtotime (' Next ') '. Self:: $WORK _day [$weekDay]  [' en '], strtotime ($start));//The next week X starts, today (Tuesday) user settings are executed every Monday, that should be performed in Monday $endTime = Strtotime ($end); $list = []; for ($i = 0;; $i + +) {$dayOfWeek = Strtotime ("+{$i} Week", $startTime);//weekly X if ($dayOFweek > $endTime) {$ break; $list [] = date (' y-m-d ', $dayOfWeek); $this->getexedate ($start, $end, $list); /** * desc Get all dates executed by x per month * @param string $start start date, 2016-10-17 PNs * @param stri Ng $end End Date, 2016-10-17 * @param int $monthDay 1~28 * @return Array * * * Public function get Monthlybuydate ($start, $end, $monthDay) $monthDay = Str_pad ($monthDay, 2, ' 0 ', str_pad_left);//Left 4 4 $start = Empty ($start)? Date (' y-m-d '): $start; $startTime = Strtotime ($start); $startDay = substr ($start, 8, 2); if (strcmp ($startDay, $monthDay) < 0) {$startMonthDayTime = Strtotime (' y-m-', Strto Time ($start)). $monthDay);  Started this month, today (for example, number 26th) user set 28th per month execution, then this month to start executing the "else {$startMonthDayTime = Strtotime (' y-m-', Strtotime (' +1 month ', $startTime)). $monthDay); Starting next month, the $endTime = Strtotime ($end); $list = []; ($i = 0;; $i + +) {$dayOfMonth = Strtotime ("+{$i} Month", $startMonthDayTime);//monthly X ($dayOfMonth &G T $endTime) {break; $list [] = date (' y-m-d ', $dayOfMonth); $this->getexedate ($start, $end, $list); /** * Desc Returns a list of execution dates deferred * @param string $start start date, 2016-10-17 * @param s Tring $end End Date, 2016-10-17 * @param array $planDate * @return Array * * *-Public function get Exedate ($start, $end, $planDate) 76 {77//Get all trading days $allTradeDate = $this->gettradedate ($start, $ End); 79 80//Take the last trading day Bayi $endTradeDate = end ($allTradeDate);   return format: [' 2016-11-01 ' = ' 2016-11-01 ', ' 2016-11-02 ' = ' 2016-11-02 ', ...], redundant, easy to program 82 83      $exeDate = [];                 Execution date after rollover ($planDate as $date) {!empty ($allTradeDate [$date]) {86 $exeDate [$date] = $date;  No else {//not found, need to be deferred to the next trading day $exeDate [$date] = "; = Strtotime ($endTradeDate); $currentTime = Strtotime ($date);                     ($i = $currentTime; $i <= $endTradeTime; $i +=86400) {//day after day until the next trading day is found until the last trading day of database storage 94 $tmpDate = Date (' y-m-d ', $i); if (!empty ($allTradeDate [$tmpDate])) {//Found on exit loop $exeDate [$date] = $tmpD Ate a break; 98}}100}101}102 103 return $exeDate; 104}

which

1 public static $WORK _day = [2         1 = [' en ' = ' Monday ', ' cn ' = ' One '],3         2 = ' en ' = ' Tuesday ', ' CN ' = ' two '],4         3 = [' en ' = ' Wednesday ', ' cn ' = ' Three '],5         4 = [' en ' = ' Thursday ', ' cn ' = ' Four '],6         5 = [' en ' = ' Friday ', ' cn ' = ' five ']7     ];

PHP for a period of time each week, the number of months, encountered special days on the back

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.