3v4l.org

run code in 300+ PHP versions simultaneously
<?php // the month to render and day to highlight $today = new DateTime(); // we'll need these to check if we're at the day we need to highlight // or if we're rendering a date that's outside $today's month $currentDay = $today->format('j'); $currentMonth = $today->format('n'); $daysInCurrentMonth = $today->format('t'); // figure out what Monday needs to kick off our table $date = (clone $today)->modify('first day of this month'); if ($date->format('w') != 1) { $date->modify('previous monday'); } $shouldStopRendering = false; echo '<table border="1">' . PHP_EOL; while (!$shouldStopRendering) { $weekDay = $date->format('w'); $month = $date->format('n'); $day = $date->format('j'); // start a new table row every time we hit a Monday, note that // since we forced $date to be a Monday above, our table should // now always start with a <tr> if ($weekDay == 1) { echo ' <tr>' . PHP_EOL; } if ($month != $currentMonth) { // we're either at the beginning or end of our table echo ' <td>&nbsp;</td>' . PHP_EOL; } else if ($day == $currentDay) { // highlight the current date echo ' <td bgcolor="#ff0000">' . $day . '</td>' . PHP_EOL; } else { echo ' <td>' . $day . '</td>' . PHP_EOL; } if ($weekDay == 0) { // every time we hit a Sunday, close the current table row echo ' </tr>' . PHP_EOL; // on a Sunday, if we've gone outside the current month **or** // this Sunday happens to be the last day we need to render, // stop looping so we can close the table if ($month != $currentMonth || $day == $daysInCurrentMonth) { $shouldStopRendering = true; } } // move on to the next day we need to display $date->modify('+1 day'); } echo '</table>';
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
<table border="1"> <tr> <td>&nbsp;</td> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> <td>6</td> </tr> <tr> <td>7</td> <td>8</td> <td>9</td> <td>10</td> <td>11</td> <td>12</td> <td>13</td> </tr> <tr> <td>14</td> <td>15</td> <td>16</td> <td>17</td> <td>18</td> <td>19</td> <td>20</td> </tr> <tr> <td>21</td> <td bgcolor="#ff0000">22</td> <td>23</td> <td>24</td> <td>25</td> <td>26</td> <td>27</td> </tr> <tr> <td>28</td> <td>29</td> <td>30</td> <td>31</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> </table>
Output for 5.6.0 - 5.6.40
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in /in/Z89i8 on line 12
Process exited with code 255.

preferences:
127.04 ms | 407 KiB | 5 Q