我想知道当前星期一(或任何其他日子)的每个星期一的日期。
我试过这个:
return date( 'm/d/Y', strtotime( "$passed_day next week" ) );
答案 0 :(得分:3)
您可以在创建或修改relative date/time formats个对象时使用DateTime。
$date = new DateTime('first Monday of this month');
$thisMonth = $date->format('m');
while ($date->format('m') === $thisMonth) {
echo $date->format('Y-m-d'), "\n";
$date->modify('next Monday');
}
2015-03-02
2015-03-09
2015-03-16
2015-03-23
2015-03-30
答案 1 :(得分:1)
保罗答案的变体,使用DateInterval和DatePeriod
$begin = new DateTime('First monday of this month');
$end = new DateTime('First monday of next month');
// Every week
$interval = new DateInterval( 'P1W' );
$daterange = new DatePeriod( $begin, $interval ,$end );
foreach($daterange as $date){
echo $date->format('Y-m-d');
}
答案 2 :(得分:0)
或使用默认日期()功能:
function mondays ( $date ) {
$mondays = [ ];
$start = date( 'N', strtotime( $date ) );
$month = date( 'm', strtotime( $date ) );
$closest_monday = strtotime( $date ) - ( $start - 1 ) * 60 * 60 * 24;
if ( $month == date( 'm', $closest_monday ) ) {
$mondays[ $closest_monday ] = date( 'N d-m-Y', $closest_monday );
} else {
$closest_monday = strtotime( $date ) + ( 8 - $start ) * 60 * 60 * 24;
$mondays[ $closest_monday ] = date( 'N d-m-Y', $closest_monday );
}
for ( $i = 1; $i < 6; $i++ ) {
$prev = $closest_monday - $i * 7 * 60 * 60 * 24;
$next = $closest_monday + $i * 7 * 60 * 60 * 24;
if ( $month == date( 'm', $next ) ) {
$mondays[ $next ] = date( 'N d-m-Y', $next );
}
if ( $month == date( 'm', $prev ) ) {
$mondays[ $prev ] = date( 'N d-m-Y', $prev );
}
}
ksort( $mondays );
return $mondays;
}
$ date = '02 -02-2014';
的输出array (size=4)
1391382000 => string '1 03-02-2014' (length=12)
1391986800 => string '1 10-02-2014' (length=12)
1392591600 => string '1 17-02-2014' (length=12)
1393196400 => string '1 24-02-2014' (length=12)