假设我有一个初始日期,其年份早于当年的年份,我想每7天重复一次,但仅限于当年。
我如何找到当年的第一次出现?
我意识到我可以用这样的循环来做到这一点:
$reOccurringEvent =new DateTime('2013-12-01');
$interval = new DateInterval('P7D');
while($reOccurringEvent->format('Y') < date('Y') ){
$reOccurringEvent->add($interval);
}
echo $reOccurringEvent->format('d m Y'); //05 01 2014
但是我觉得应该有一种更有效的方法来实现这一目标,而不是在日期中重复添加一个间隔(如果初始日期是几年前会发生很多次)。
我希望能够计算应该添加间隔的次数,并且只需要一次。
我在想这样的事情:
$date = new DateTime();
$diff = $date->diff($reOccurringEvent)->days%7;
但显然这不起作用,我无法弄清楚如何做到这一点的逻辑。
答案 0 :(得分:1)
我认为,如果你每隔7天做一次,你可以找到你的初始日期的星期几,然后在星期几那天得到当年的第一个日期......
查找星期几:How to find the day of week from a date using PHP?
查找今年某一天的日期:Getting first weekday in a month with strtotime
把它放在一起:
$date=Date("2/8/2012");
//Get the day of week for the date in question
$dayOfWeek = date('l', strtotime($date));
echo "The day of week for the given date is: $dayOfWeek <br>";
//Get the current year
$thisYear = date("Y");
echo "This year: $thisYear <br>";
//Create a date with the first occurence of the day of week of the given date for the current year
$firstOccurenceThisYear = date("m/d/y", strtotime("January " .$thisYear ." " . $dayOfWeek));
echo "The first interval of the year is: $firstOccurenceThisYear";
/*
Output:
This year: 2014
The day of week for the given date is: Wednesday
The first interval of the year is: 01/01/14
*/
答案 1 :(得分:1)
更一般地说,算法是找到给定日期和去年最后一天之间的间隔数。然后将间隔乘以间隔数+ 1,得到当前年份的第一个间隔。
$date1="12/9/2013";
$ts1 = strtotime($date1);
$ts2 = strtotime("12/31/" . Date("Y")-1);
//get the number of seconds between the date and first of the year
$seconds_diff = $ts2 - $ts1;
echo "$seconds_diff <br>";
//get the number of days
$dayDiff=$seconds_diff/86400;
//how many intervals?
$intervalDays = "10";
//get the number of intervals from start date to last day of last year
$numIntervals = floor($dayDiff/$intervalDays);
echo $numIntervals."<br>";
//now the total intervals to get into the current year is one more interval, turn this into days
$totIntervals= ($numIntervals* $intervalDays)+$intervalDays;
//Date Time date in question
$theDt = new DateTime($date1);
//Add the intervals we calculated to the date in question, and we have the first date of the interval for the current year...
$theDt->add(new DateInterval('P' . $totIntervals. 'D'));
echo "The first date of the intreval is: " . $theDt->format('Y-m-d');
答案 2 :(得分:1)
以下是@ Dan的第二个答案的略微修改版本,对我来说效果很好。
基准如下所示。
$date="1985-02-18";
$intervalDays = "5";
//original version
$benchMark = microtime(true);
$dt1 = new DateTime($date);
$interval = new DateInterval("P{$intervalDays}D");
while ($dt1->format('Y') < date('Y')) {
$dt1->add($interval);
}
echo $dt1->format('d m Y') . '<br>';
echo microtime(true)-$benchMark.'<br>';
//new version
$benchMark = microtime(true);
$dt1 = new DateTime($date);
$dt2 = new DateTime("12/31/" . ((int) Date("Y") - 1));
$dayDiff = $dt1->diff($dt2)->days;
$numIntervals = floor($dayDiff / $intervalDays);
$totIntervals = ($numIntervals * $intervalDays) + $intervalDays;
$dt1->add(new DateInterval('P' . $totIntervals . 'D'));
echo $dt1->format('d m Y').'<br>';
echo microtime(true)-$benchMark.'<br>';
exit;
<强>输出强>
02 01 2014
0.0145111083984
02 01 2014
0.000123977661133