3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Example 1 print_r(getPeroid()); print_r(getPeroid()->start); // Example 2 print_r(getPeroid(new DateTime("2013-02-14"))); // Example 3 print_r(getPeriodBetween(new DateTime("2013-1-1"), new DateTime())); function getPeroid(\Datetime $date = null, DateInterval $interval = null) { $interval = $interval ? : new DateInterval('P7D'); $end = $date ? : new DateTime(); $end->modify("Last Sunday"); $start = clone $end; $start->sub($interval); return (object) array( "start" => $start->format("l, d F Y"), "end" => $end->format("l, d F Y") ); } function getPeriodBetween(Datetime $start = null, Datetime $end = null, DateInterval $interval = null) { $interval = $interval ? : new DateInterval('P7D'); if ($start === null) { $start = new DateTime(); $start->modify("First Sunday of January"); } else { $start->modify("First Sunday"); } if ($end === null) { $end = new DateTime(); $end->modify("Last Sunday of December"); } $range = array(); while ( $start < $end ) { $r = (object) array( "start" => $start->format("l, d F Y") ); $start->add($interval); $r->end = $start->format("l, d F Y"); $range[] = $r; } return $range; } ?>
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.36, 5.6.0 - 5.6.22, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
stdClass Object ( [start] => Sunday, 31 March 2013 [end] => Sunday, 07 April 2013 ) Sunday, 31 March 2013stdClass Object ( [start] => Sunday, 03 February 2013 [end] => Sunday, 10 February 2013 ) Array ( [0] => stdClass Object ( [start] => Sunday, 06 January 2013 [end] => Sunday, 13 January 2013 ) [1] => stdClass Object ( [start] => Sunday, 13 January 2013 [end] => Sunday, 20 January 2013 ) [2] => stdClass Object ( [start] => Sunday, 20 January 2013 [end] => Sunday, 27 January 2013 ) [3] => stdClass Object ( [start] => Sunday, 27 January 2013 [end] => Sunday, 03 February 2013 ) [4] => stdClass Object ( [start] => Sunday, 03 February 2013 [end] => Sunday, 10 February 2013 ) [5] => stdClass Object ( [start] => Sunday, 10 February 2013 [end] => Sunday, 17 February 2013 ) [6] => stdClass Object ( [start] => Sunday, 17 February 2013 [end] => Sunday, 24 February 2013 ) [7] => stdClass Object ( [start] => Sunday, 24 February 2013 [end] => Sunday, 03 March 2013 ) [8] => stdClass Object ( [start] => Sunday, 03 March 2013 [end] => Sunday, 10 March 2013 ) [9] => stdClass Object ( [start] => Sunday, 10 March 2013 [end] => Sunday, 17 March 2013 ) [10] => stdClass Object ( [start] => Sunday, 17 March 2013 [end] => Sunday, 24 March 2013 ) [11] => stdClass Object ( [start] => Sunday, 24 March 2013 [end] => Sunday, 31 March 2013 ) [12] => stdClass Object ( [start] => Sunday, 31 March 2013 [end] => Sunday, 07 April 2013 ) [13] => stdClass Object ( [start] => Sunday, 07 April 2013 [end] => Sunday, 14 April 2013 ) )
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/hptPn on line 13 Parse error: syntax error, unexpected ':' in /in/hptPn on line 14
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/hptPn on line 13 Parse error: parse error, unexpected ':' in /in/hptPn on line 14
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /in/hptPn on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_OBJECT_OPERATOR in /in/hptPn on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/hptPn on line 5
Process exited with code 255.

preferences:
284.85 ms | 401 KiB | 374 Q