3v4l.org

run code in 300+ PHP versions simultaneously
<?php $timezone = new DateTimeZone( "UTC" ); $fmj = DateInterval::createFromDateString( 'first Monday of January next year' ); //Monday from start 2019 only $period = new DatePeriod( new DateTime( '2019-01-01 00:00:00.000000', $timezone ), $fmj, new DateTime( '2022-01-11 00:00:00.000000', $timezone ) ); //$period->current= new \DateTime('2019-05-06 00:00:00', new \DateTimeZone('UTC')); $y = 0; foreach ( $period as $dt ) { $c[] = $period; $display[$y++] = $dt->format( "l Y-m-d H:i:s" ); //stop total_recurrence if( $y == 5 ) { break; } } $var = new DateTime( 'first monday of January', new DateTimeZone( "UTC" )); $display[5] = $var->format( "l Y-m-d H:i:s" ); var_dump( $display, $fmj, $c);
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.30, 8.0.0 - 8.0.22, 8.1.0 - 8.1.9
array(5) { [0]=> string(27) "Tuesday 2019-01-01 00:00:00" [1]=> string(27) "Tuesday 2020-01-07 00:00:00" [2]=> string(29) "Wednesday 2021-01-06 00:00:00" [3]=> string(27) "Tuesday 2022-01-04 00:00:00" [5]=> string(26) "Monday 2021-01-04 00:00:00" } object(DateInterval)#2 (16) { ["y"]=> int(1) ["m"]=> int(0) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(1) ["weekday_behavior"]=> int(1) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(2) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(1) ["have_special_relative"]=> int(1) } array(4) { [0]=> object(DatePeriod)#3 (6) { ["start"]=> object(DateTime)#4 (3) { ["date"]=> string(26) "2019-01-01 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["current"]=> object(DateTime)#7 (3) { ["date"]=> string(26) "2023-01-03 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["end"]=> object(DateTime)#8 (3) { ["date"]=> string(26) "2022-01-11 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["interval"]=> object(DateInterval)#9 (16) { ["y"]=> int(1) ["m"]=> int(0) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(1) ["weekday_behavior"]=> int(1) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(2) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(1) ["have_special_relative"]=> int(1) } ["recurrences"]=> int(1) ["include_start_date"]=> bool(true) } [1]=> object(DatePeriod)#3 (6) { ["start"]=> object(DateTime)#10 (3) { ["date"]=> string(26) "2019-01-01 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["current"]=> object(DateTime)#4 (3) { ["date"]=> string(26) "2023-01-03 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["end"]=> object(DateTime)#7 (3) { ["date"]=> string(26) "2022-01-11 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["interval"]=> object(DateInterval)#8 (16) { ["y"]=> int(1) ["m"]=> int(0) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(1) ["weekday_behavior"]=> int(1) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(2) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(1) ["have_special_relative"]=> int(1) } ["recurrences"]=> int(1) ["include_start_date"]=> bool(true) } [2]=> object(DatePeriod)#3 (6) { ["start"]=> object(DateTime)#9 (3) { ["date"]=> string(26) "2019-01-01 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["current"]=> object(DateTime)#10 (3) { ["date"]=> string(26) "2023-01-03 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["end"]=> object(DateTime)#4 (3) { ["date"]=> string(26) "2022-01-11 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["interval"]=> object(DateInterval)#7 (16) { ["y"]=> int(1) ["m"]=> int(0) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(1) ["weekday_behavior"]=> int(1) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(2) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(1) ["have_special_relative"]=> int(1) } ["recurrences"]=> int(1) ["include_start_date"]=> bool(true) } [3]=> object(DatePeriod)#3 (6) { ["start"]=> object(DateTime)#8 (3) { ["date"]=> string(26) "2019-01-01 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["current"]=> object(DateTime)#9 (3) { ["date"]=> string(26) "2023-01-03 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["end"]=> object(DateTime)#10 (3) { ["date"]=> string(26) "2022-01-11 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["interval"]=> object(DateInterval)#4 (16) { ["y"]=> int(1) ["m"]=> int(0) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(1) ["weekday_behavior"]=> int(1) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(2) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(1) ["have_special_relative"]=> int(1) } ["recurrences"]=> int(1) ["include_start_date"]=> bool(true) } }

preferences:
52.54 ms | 433 KiB | 6 Q