DateTime クラスを使用したソリューションを次に示します。
/**
* Get weekly repeating dates for an event
*
* Creates an array of date time objects one for each $week
* starting at $startDate. Using the default value of 0 will return
* an array with just the $startDate, a value of 1 will return an
* array containing $startDate + the following week.
*
* @param DateTime $startDate
* @param int optional defaults to 0 number of weeks to repeat
* @return array of DateTime objects
*/
function getWeeklyOccurences(DateTime $startDate, $weeks = 0)
{
$occurences = array();
$period = new DatePeriod($startDate, new DateInterval('P1W'), $weeks);
foreach($period as $date){
$occurences[] = $date;
}
return $occurences;
}
$startDate = new datetime();
$startDate->setTime(16, 30);
var_dump(getWeeklyOccurences($startDate, 52));
次の出力が得られます:-
array (size=53)
0 =>
object(DateTime)[4]
public 'date' => string '2012-11-06 16:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
1 =>
object(DateTime)[5]
public 'date' => string '2012-11-13 16:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
2 =>
object(DateTime)[6]
public 'date' => string '2012-11-20 16:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
3 =>
object(DateTime)[7]
public 'date' => string '2012-11-27 16:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
等..
その後、必要に応じて出力をフォーマットできますDateTime::format()