3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $posts = array( array('post_id' => 7, 'timestamp' => '2014-04-20 20:17:49'), array('post_id' => 6, 'timestamp' => '2014-04-07 20:17:49'), array('post_id' => 5, 'timestamp' => '2014-03-17 20:17:49'), array('post_id' => 4, 'timestamp' => '2014-02-14 20:17:49'), array('post_id' => 3, 'timestamp' => '2014-01-09 20:17:49'), array('post_id' => 2, 'timestamp' => '2013-09-23 20:17:49'), array('post_id' => 1, 'timestamp' => '2012-09-23 20:17:49') ); $today = strtotime(date("Y-m-d")); $week_start = strtotime('last sunday', strtotime('tomorrow')); $month_start = strtotime(date_create(date("Y-m-d"))->modify('first day of this month')->format("Y-m-d")); $year_start = strtotime(date_create(date("Y-m-d"))->modify('first day of january 2014')->format("Y-m-d")); foreach ($posts as $post) { $item = strtotime($post['timestamp']); if ($item >= $today) { // Today echo '<br><br>' . 'Today' . '<br>'; echo $post['post_id'] . '<br>'; } else if (($item <= $today) && ($item >= $week_start)) { // This week echo '<br><br>' . 'This week' . '<br>'; echo $post['post_id'] . '<br>'; } else if (($item <= $week_start) && ($item >= $month_start)) { // This week echo '<br><br>' . 'This month' . '<br>'; echo $post['post_id'] . '<br>'; } else if (($item <= $month_start) && ($item >= $year_start)) { // This week echo '<br><br>' . 'This year' . '<br>'; echo $post['post_id'] . '<br>'; } }
Output for 5.3.6 - 7.1.0
<br><br>Today<br>7<br><br><br>This month<br>6<br><br><br>This year<br>5<br><br><br>This year<br>4<br><br><br>This year<br>3<br>
Output for 5.3.0 - 5.3.5
<br><br>Today<br>7<br><br><br>This month<br>6<br>
Output for 5.2.13 - 5.2.17
Warning: DateTime::modify(): Failed to parse time string (first day of this month) at position 10 (o): The timezone could not be found in the database in /in/pCFt6 on line 15 Fatal error: Call to a member function format() on a non-object in /in/pCFt6 on line 15
Process exited with code 255.
Output for 5.2.0 - 5.2.12
Fatal error: Call to a member function format() on a non-object in /in/pCFt6 on line 15
Process exited with code 255.
Output for 5.0.0 - 5.1.6
Fatal error: Call to undefined function date_create() in /in/pCFt6 on line 15
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /in/pCFt6 on line 15
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_OBJECT_OPERATOR in /in/pCFt6 on line 15
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/pCFt6 on line 15
Process exited with code 255.