按出现次数重复事件

时间:2014-05-12 10:10:18

标签: php

<?php
function getDateOfWeekDay($day) {
    $weekDays = array(
      'Sunday',
      'Monday',
      'Tuesday',
      'Wednesday',
      'Thursday',
      'Friday',
      'Saturday',
    );

    $dayNumber = array_search($day, $weekDays);
    $currentDayNumber =  date('w', strtotime('today'));

   // if ($dayNumber > $currentDayNumber) {
 if ( $currentDayNumber > $dayNumber) {
      //return date('Y-m-d', strtotime($day));
       echo date('Y-m-d',strtotime($day.' this week'));
    } else {
      //return date('Y-m-d', strtotime($day) + 604800);
echo date('Y-m-d',strtotime($day.' next week') +1);
    }
  }


 $ron="0,2,6"; // $ron is repeats on every sunday , tuesday, and saturday
 $oc=4; // $oc is an occurences how many time it will repeat
 $ronS=explode(',', $ron);
 foreach($ronS as $ronS) {
   $res[]= $ronS;
 }
 $r=0; 
 $rr=0;
 for($i = 0; $i < $oc; $i++)
 {
      if($i<count($res))
                     {
                      $t=$res[$i];
                      $r++;
                     }
                     else
                     {
                      $t=$res[$rr];
                      $rr++;
                     }
                     $week = array('0'=>"Sunday","1"=>"Monday","2"=>"Tuesday","3"=>"Wednesday","4"=>"Thursday","5"=>"Friday","6"=>"Saturday");
                     $day[] = $week[$t];
 }//for
print_r($day);
 foreach ($day  as $da)
 {
   echo  getDateOfWeekDay($da)."\n";
 }
?>

此代码的输出是

Array
(
    [0] => Sunday
    [1] => Tuesday
    [2] => Saturday
    [3] => Sunday
)
2014-05-18
2014-05-20
2014-05-24
2014-05-18

所有工作正常,但最后一个日期 2014-05-18 是错误的2014-05-25

任何想法的朋友

0 个答案:

没有答案