3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getTimeRange($month) { $month = str_pad($month, 2, '0', STR_PAD_LEFT); $year = $month > date('m') ? date('Y') - 1 : date('Y'); $start_date = "{$year}-{$month}-01"; $end_date = date('Y-m-d', strtotime("+1 month", strtotime($start_date))); return array( 'start_date' => $start_date, 'end_date' => $end_date ); } function getTimeRange2($month, $year = 0) { $month = str_pad($month, 2, '0', STR_PAD_LEFT); $year = ($year ? : ($month > date('m') ? date('Y') - 1 : date('Y'))); $res = array( 'start_date' => "{$year}-{$month}-01", 'end_date' => date('Y-m-d', strtotime("+1 month", strtotime($res['start_date']))) ); return $res; } var_dump(getTimeRange(1)); var_dump(getTimeRange2(1));
Output for 8.3.0 - 8.3.4, 8.3.6
array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "2017-02-01" } Warning: Undefined variable $res in /in/Z68l9 on line 20 Warning: Trying to access array offset on null in /in/Z68l9 on line 20 Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated in /in/Z68l9 on line 20 array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "1970-02-01" }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "2017-02-01" } Warning: Undefined variable $res in /in/Z68l9 on line 20 Warning: Trying to access array offset on null in /in/Z68l9 on line 20 Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated in /in/Z68l9 on line 20 array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "1970-02-01" }
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18
array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "2017-02-01" } Warning: Undefined variable $res in /in/Z68l9 on line 20 Warning: Trying to access array offset on value of type null in /in/Z68l9 on line 20 Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated in /in/Z68l9 on line 20 array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "1970-02-01" }
Output for 8.0.0 - 8.0.30
array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "2017-02-01" } Warning: Undefined variable $res in /in/Z68l9 on line 20 Warning: Trying to access array offset on value of type null in /in/Z68l9 on line 20 array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "1970-02-01" }
Output for 7.4.0 - 7.4.33
array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "2017-02-01" } Notice: Undefined variable: res in /in/Z68l9 on line 20 Notice: Trying to access array offset on value of type null in /in/Z68l9 on line 20 array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "1970-02-01" }
Output for 7.3.32 - 7.3.33
array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "2017-02-01" } array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "1970-02-01" }
Output for 5.6.0 - 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31
array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "2017-02-01" } Notice: Undefined variable: res in /in/Z68l9 on line 20 array(2) { ["start_date"]=> string(10) "2017-01-01" ["end_date"]=> string(10) "1970-02-01" }

preferences:
214.08 ms | 403 KiB | 255 Q