3v4l.org

run code in 500+ PHP versions simultaneously
<?php function DateSameMonth($date_iso, $add_sub_months = 1, $operator = "+") { $mdate = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; $date = new DateTime($date_iso); $d = (int)$date->format('d'); $m = (int)$date->format('m'); $y = (int)$date->format('Y'); if ($m == 2) { $mdate[$m] = (($y % 4) === 0) ? (($d <= 29) ? $d : 29) : (($d <= 28) ? $d : 28); } if($d == 1) { $mod = "first day of "; } elseif($d == $mdate[$m]) { $mod = "last day of "; } else { $mod = ""; } $date->modify($mod . $operator . $add_sub_months . ' months'); return $date->format("Y-m-d"); } var_dump(DateSameMonth("2022-01-31", 3, "-")); var_dump(DateSameMonth("2022-11-30", 3, "+")); var_dump(DateSameMonth("2022-01-31", 25, "+")); //leap year var_dump(DateSameMonth("2022-01-16", 25, "+")); echo "\n\nTEST: FIRST OF (+1 month)\n"; for($i=1; $i<=12; $i++){ $start = "2023-". $i ."-01"; echo $start . " ----> ". DateSameMonth($start, 1, "+") . "\n"; } echo "\n\nTEST: LAST OF (+12 months)\n"; for($i=1; $i<=12; $i++){ $start = date("Y-m-t", strtotime("2023-". $i ."-01")); echo $start . " ----> ". DateSameMonth($start, 12, "+") . "\n"; } echo "\n\nTEST: RANDOM DAY (+1 month)\n"; for($i=1; $i<=50; $i++){ $start = date("Y-m-") . rand(1, 27); echo $start . " ----> ". DateSameMonth($start, 1, "+") . "\n"; } echo "\n\nTEST: RANDOM DAY (-1 month)\n"; for($i=1; $i<=50; $i++){ $start = date("Y-m-") . rand(1, 27); echo $start . " ----> ". DateSameMonth($start, 1, "-") . "\n"; }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.2.40.0130.00917.84
8.2.30.0130.00717.72
8.2.20.0180.00417.72
8.2.10.0190.00017.72
8.2.00.0180.00017.74
8.1.170.0140.00417.72
8.1.160.0120.00617.72
8.1.150.0170.00417.72
8.1.140.0140.00317.72
8.1.130.0140.00317.72
8.1.120.0170.00017.72
8.1.110.0090.00917.72
8.1.100.0120.00617.72
8.1.90.0110.00617.73
8.1.80.0140.00317.72
8.1.70.0150.00317.72
8.1.60.0130.00617.72
8.1.50.0120.00617.73
8.1.40.0150.00517.84
8.1.30.0100.00817.72
8.1.20.0130.00517.72
8.1.10.0150.00317.72
8.1.00.0120.00617.72
8.0.280.0130.00517.72
8.0.270.0110.00717.72
8.0.260.0150.00317.72
8.0.250.0110.00417.72
8.0.240.0140.00317.72
8.0.230.0090.00917.72
8.0.220.0160.00317.72
8.0.210.0090.00717.72
8.0.200.0110.01117.72
8.0.190.0140.00717.72
8.0.180.0190.00017.72
8.0.170.0160.00317.72
8.0.160.0100.00617.72
8.0.150.0170.00317.72
8.0.140.0120.00617.72
8.0.130.0150.00217.72
8.0.120.0180.00017.72
8.0.110.0110.00617.72
8.0.100.0100.00717.72
8.0.90.0110.00617.72
8.0.80.0110.00617.72
8.0.70.0120.00617.72
8.0.60.0110.00817.72
8.0.50.0120.00617.72
8.0.30.0080.00817.72
8.0.20.0140.00317.72
8.0.10.0110.00617.72
5.4.450.0160.00416.20

preferences:
49.58 ms | 903 KiB | 5 Q