3v4l.org

run code in 300+ 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"; }
Output for git.master
string(10) "2021-10-31" string(10) "2023-02-28" string(10) "2024-02-29" string(10) "2024-02-16" TEST: FIRST OF (+1 month) 2023-1-01 ----> 2023-02-01 2023-2-01 ----> 2023-03-01 2023-3-01 ----> 2023-04-01 2023-4-01 ----> 2023-05-01 2023-5-01 ----> 2023-06-01 2023-6-01 ----> 2023-07-01 2023-7-01 ----> 2023-08-01 2023-8-01 ----> 2023-09-01 2023-9-01 ----> 2023-10-01 2023-10-01 ----> 2023-11-01 2023-11-01 ----> 2023-12-01 2023-12-01 ----> 2024-01-01 TEST: LAST OF (+12 months) 2023-01-31 ----> 2024-01-31 2023-02-28 ----> 2024-02-29 2023-03-31 ----> 2024-03-31 2023-04-30 ----> 2024-04-30 2023-05-31 ----> 2024-05-31 2023-06-30 ----> 2024-06-30 2023-07-31 ----> 2024-07-31 2023-08-31 ----> 2024-08-31 2023-09-30 ----> 2024-09-30 2023-10-31 ----> 2024-10-31 2023-11-30 ----> 2024-11-30 2023-12-31 ----> 2024-12-31 TEST: RANDOM DAY (+1 month) 2023-04-20 ----> 2023-05-20 2023-04-23 ----> 2023-05-23 2023-04-15 ----> 2023-05-15 2023-04-7 ----> 2023-05-07 2023-04-21 ----> 2023-05-21 2023-04-27 ----> 2023-05-27 2023-04-5 ----> 2023-05-05 2023-04-12 ----> 2023-05-12 2023-04-18 ----> 2023-05-18 2023-04-10 ----> 2023-05-10 2023-04-5 ----> 2023-05-05 2023-04-11 ----> 2023-05-11 2023-04-10 ----> 2023-05-10 2023-04-4 ----> 2023-05-04 2023-04-25 ----> 2023-05-25 2023-04-14 ----> 2023-05-14 2023-04-26 ----> 2023-05-26 2023-04-16 ----> 2023-05-16 2023-04-18 ----> 2023-05-18 2023-04-15 ----> 2023-05-15 2023-04-21 ----> 2023-05-21 2023-04-14 ----> 2023-05-14 2023-04-21 ----> 2023-05-21 2023-04-26 ----> 2023-05-26 2023-04-17 ----> 2023-05-17 2023-04-4 ----> 2023-05-04 2023-04-11 ----> 2023-05-11 2023-04-27 ----> 2023-05-27 2023-04-16 ----> 2023-05-16 2023-04-9 ----> 2023-05-09 2023-04-8 ----> 2023-05-08 2023-04-11 ----> 2023-05-11 2023-04-27 ----> 2023-05-27 2023-04-15 ----> 2023-05-15 2023-04-2 ----> 2023-05-02 2023-04-16 ----> 2023-05-16 2023-04-23 ----> 2023-05-23 2023-04-24 ----> 2023-05-24 2023-04-6 ----> 2023-05-06 2023-04-4 ----> 2023-05-04 2023-04-11 ----> 2023-05-11 2023-04-17 ----> 2023-05-17 2023-04-14 ----> 2023-05-14 2023-04-16 ----> 2023-05-16 2023-04-9 ----> 2023-05-09 2023-04-17 ----> 2023-05-17 2023-04-18 ----> 2023-05-18 2023-04-18 ----> 2023-05-18 2023-04-25 ----> 2023-05-25 2023-04-13 ----> 2023-05-13 TEST: RANDOM DAY (-1 month) 2023-04-23 ----> 2023-03-23 2023-04-14 ----> 2023-03-14 2023-04-13 ----> 2023-03-13 2023-04-20 ----> 2023-03-20 2023-04-1 ----> 2023-03-01 2023-04-27 ----> 2023-03-27 2023-04-2 ----> 2023-03-02 2023-04-16 ----> 2023-03-16 2023-04-17 ----> 2023-03-17 2023-04-11 ----> 2023-03-11 2023-04-23 ----> 2023-03-23 2023-04-14 ----> 2023-03-14 2023-04-15 ----> 2023-03-15 2023-04-3 ----> 2023-03-03 2023-04-24 ----> 2023-03-24 2023-04-26 ----> 2023-03-26 2023-04-17 ----> 2023-03-17 2023-04-2 ----> 2023-03-02 2023-04-16 ----> 2023-03-16 2023-04-24 ----> 2023-03-24 2023-04-8 ----> 2023-03-08 2023-04-11 ----> 2023-03-11 2023-04-1 ----> 2023-03-01 2023-04-27 ----> 2023-03-27 2023-04-14 ----> 2023-03-14 2023-04-24 ----> 2023-03-24 2023-04-9 ----> 2023-03-09 2023-04-17 ----> 2023-03-17 2023-04-12 ----> 2023-03-12 2023-04-27 ----> 2023-03-27 2023-04-24 ----> 2023-03-24 2023-04-15 ----> 2023-03-15 2023-04-15 ----> 2023-03-15 2023-04-6 ----> 2023-03-06 2023-04-7 ----> 2023-03-07 2023-04-12 ----> 2023-03-12 2023-04-25 ----> 2023-03-25 2023-04-6 ----> 2023-03-06 2023-04-22 ----> 2023-03-22 2023-04-9 ----> 2023-03-09 2023-04-9 ----> 2023-03-09 2023-04-21 ----> 2023-03-21 2023-04-19 ----> 2023-03-19 2023-04-21 ----> 2023-03-21 2023-04-9 ----> 2023-03-09 2023-04-13 ----> 2023-03-13 2023-04-4 ----> 2023-03-04 2023-04-11 ----> 2023-03-11 2023-04-5 ----> 2023-03-05 2023-04-11 ----> 2023-03-11
Output for git.master_jit
string(10) "2021-10-31" string(10) "2023-02-28" string(10) "2024-02-29" string(10) "2024-02-16" TEST: FIRST OF (+1 month) 2023-1-01 ----> 2023-02-01 2023-2-01 ----> 2023-03-01 2023-3-01 ----> 2023-04-01 2023-4-01 ----> 2023-05-01 2023-5-01 ----> 2023-06-01 2023-6-01 ----> 2023-07-01 2023-7-01 ----> 2023-08-01 2023-8-01 ----> 2023-09-01 2023-9-01 ----> 2023-10-01 2023-10-01 ----> 2023-11-01 2023-11-01 ----> 2023-12-01 2023-12-01 ----> 2024-01-01 TEST: LAST OF (+12 months) 2023-01-31 ----> 2024-01-31 2023-02-28 ----> 2024-02-29 2023-03-31 ----> 2024-03-31 2023-04-30 ----> 2024-04-30 2023-05-31 ----> 2024-05-31 2023-06-30 ----> 2024-06-30 2023-07-31 ----> 2024-07-31 2023-08-31 ----> 2024-08-31 2023-09-30 ----> 2024-09-30 2023-10-31 ----> 2024-10-31 2023-11-30 ----> 2024-11-30 2023-12-31 ----> 2024-12-31 TEST: RANDOM DAY (+1 month) 2023-04-18 ----> 2023-05-18 2023-04-13 ----> 2023-05-13 2023-04-12 ----> 2023-05-12 2023-04-11 ----> 2023-05-11 2023-04-11 ----> 2023-05-11 2023-04-4 ----> 2023-05-04 2023-04-6 ----> 2023-05-06 2023-04-6 ----> 2023-05-06 2023-04-8 ----> 2023-05-08 2023-04-4 ----> 2023-05-04 2023-04-6 ----> 2023-05-06 2023-04-11 ----> 2023-05-11 2023-04-8 ----> 2023-05-08 2023-04-17 ----> 2023-05-17 2023-04-13 ----> 2023-05-13 2023-04-8 ----> 2023-05-08 2023-04-25 ----> 2023-05-25 2023-04-2 ----> 2023-05-02 2023-04-23 ----> 2023-05-23 2023-04-23 ----> 2023-05-23 2023-04-18 ----> 2023-05-18 2023-04-1 ----> 2023-05-01 2023-04-26 ----> 2023-05-26 2023-04-7 ----> 2023-05-07 2023-04-7 ----> 2023-05-07 2023-04-3 ----> 2023-05-03 2023-04-19 ----> 2023-05-19 2023-04-19 ----> 2023-05-19 2023-04-27 ----> 2023-05-27 2023-04-4 ----> 2023-05-04 2023-04-3 ----> 2023-05-03 2023-04-15 ----> 2023-05-15 2023-04-3 ----> 2023-05-03 2023-04-19 ----> 2023-05-19 2023-04-13 ----> 2023-05-13 2023-04-17 ----> 2023-05-17 2023-04-18 ----> 2023-05-18 2023-04-15 ----> 2023-05-15 2023-04-12 ----> 2023-05-12 2023-04-19 ----> 2023-05-19 2023-04-4 ----> 2023-05-04 2023-04-18 ----> 2023-05-18 2023-04-22 ----> 2023-05-22 2023-04-5 ----> 2023-05-05 2023-04-1 ----> 2023-05-01 2023-04-20 ----> 2023-05-20 2023-04-20 ----> 2023-05-20 2023-04-7 ----> 2023-05-07 2023-04-8 ----> 2023-05-08 2023-04-12 ----> 2023-05-12 TEST: RANDOM DAY (-1 month) 2023-04-17 ----> 2023-03-17 2023-04-20 ----> 2023-03-20 2023-04-10 ----> 2023-03-10 2023-04-23 ----> 2023-03-23 2023-04-6 ----> 2023-03-06 2023-04-19 ----> 2023-03-19 2023-04-25 ----> 2023-03-25 2023-04-22 ----> 2023-03-22 2023-04-3 ----> 2023-03-03 2023-04-19 ----> 2023-03-19 2023-04-4 ----> 2023-03-04 2023-04-20 ----> 2023-03-20 2023-04-1 ----> 2023-03-01 2023-04-20 ----> 2023-03-20 2023-04-23 ----> 2023-03-23 2023-04-4 ----> 2023-03-04 2023-04-1 ----> 2023-03-01 2023-04-21 ----> 2023-03-21 2023-04-20 ----> 2023-03-20 2023-04-20 ----> 2023-03-20 2023-04-18 ----> 2023-03-18 2023-04-10 ----> 2023-03-10 2023-04-6 ----> 2023-03-06 2023-04-16 ----> 2023-03-16 2023-04-24 ----> 2023-03-24 2023-04-7 ----> 2023-03-07 2023-04-7 ----> 2023-03-07 2023-04-2 ----> 2023-03-02 2023-04-18 ----> 2023-03-18 2023-04-13 ----> 2023-03-13 2023-04-17 ----> 2023-03-17 2023-04-13 ----> 2023-03-13 2023-04-13 ----> 2023-03-13 2023-04-21 ----> 2023-03-21 2023-04-4 ----> 2023-03-04 2023-04-18 ----> 2023-03-18 2023-04-14 ----> 2023-03-14 2023-04-15 ----> 2023-03-15 2023-04-11 ----> 2023-03-11 2023-04-20 ----> 2023-03-20 2023-04-17 ----> 2023-03-17 2023-04-16 ----> 2023-03-16 2023-04-7 ----> 2023-03-07 2023-04-11 ----> 2023-03-11 2023-04-14 ----> 2023-03-14 2023-04-1 ----> 2023-03-01 2023-04-12 ----> 2023-03-12 2023-04-22 ----> 2023-03-22 2023-04-4 ----> 2023-03-04 2023-04-2 ----> 2023-03-02

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
25.68 ms | 422 KiB | 5 Q