3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Get Dates In Month. * * @updated 2022-12-15 19:09:31 +07:00 * * @param bool|string $yearmonth * true = current year/month * string = other year/month * * @note If using PHP < 8, remove the bool|string union type signature! * @return array */ function get_dates_in_month(bool|string $yearmonth): array { /* If for current year/month */ if($yearmonth === true) { $base = date('Y-m'); // Current base date $days = range(1, date('d')); // Days from 1 to now } /* If for other year/month */ else { $base = $yearmonth; // Literal base date $days = range(1, date('t', strtotime($yearmonth))); // Days from 1 to month max } /* Combine with day added */ $dates = array_map(function($day) use ($base) { return $base . '-' . str_pad((string) $day, 2, "0", STR_PAD_LEFT); }, $days); return $dates; } echo "==== Current Y/M ====\n"; var_dump(get_dates_in_month(true)); echo "==== Other Y/M ====\n"; var_dump(get_dates_in_month('1980-02'));
Output for 8.1.23 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
==== Current Y/M ==== array(15) { [0]=> string(10) "2022-12-01" [1]=> string(10) "2022-12-02" [2]=> string(10) "2022-12-03" [3]=> string(10) "2022-12-04" [4]=> string(10) "2022-12-05" [5]=> string(10) "2022-12-06" [6]=> string(10) "2022-12-07" [7]=> string(10) "2022-12-08" [8]=> string(10) "2022-12-09" [9]=> string(10) "2022-12-10" [10]=> string(10) "2022-12-11" [11]=> string(10) "2022-12-12" [12]=> string(10) "2022-12-13" [13]=> string(10) "2022-12-14" [14]=> string(10) "2022-12-15" } ==== Other Y/M ==== array(29) { [0]=> string(10) "1980-02-01" [1]=> string(10) "1980-02-02" [2]=> string(10) "1980-02-03" [3]=> string(10) "1980-02-04" [4]=> string(10) "1980-02-05" [5]=> string(10) "1980-02-06" [6]=> string(10) "1980-02-07" [7]=> string(10) "1980-02-08" [8]=> string(10) "1980-02-09" [9]=> string(10) "1980-02-10" [10]=> string(10) "1980-02-11" [11]=> string(10) "1980-02-12" [12]=> string(10) "1980-02-13" [13]=> string(10) "1980-02-14" [14]=> string(10) "1980-02-15" [15]=> string(10) "1980-02-16" [16]=> string(10) "1980-02-17" [17]=> string(10) "1980-02-18" [18]=> string(10) "1980-02-19" [19]=> string(10) "1980-02-20" [20]=> string(10) "1980-02-21" [21]=> string(10) "1980-02-22" [22]=> string(10) "1980-02-23" [23]=> string(10) "1980-02-24" [24]=> string(10) "1980-02-25" [25]=> string(10) "1980-02-26" [26]=> string(10) "1980-02-27" [27]=> string(10) "1980-02-28" [28]=> string(10) "1980-02-29" }
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
127.38 ms | 409 KiB | 5 Q