3v4l.org

run code in 300+ PHP versions simultaneously
<?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>'; } }

preferences:
42.87 ms | 402 KiB | 5 Q