3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * @Class : IntervalDate * @Autor : FelipeBarros<felipe.barros.pt@gmail.com> * @Description : Servico responsavel retornar os dias da semana entre o intervalo de duas datas * @Version : 1.0 [2014-09-14] * */ class IntervalDate { private $week_days = [ 'mon' => 'monday', 'tue' => 'tuesday', 'wed' => 'wednesday', 'thu' => 'thursday', 'fri' => 'friday', 'sat' => 'saturday', 'sun' => 'sunday' ]; private $start_date; private $final_date; private $n_diff_days; private $format = 'Y-m-d'; private $result = []; public function __construct($start_date, $final_date) { $this->start_date = new \DateTime($start_date); $this->final_date = new \DateTime($final_date); $this->n_diff_days = $this->final_date->diff($this->start_date)->format('%d'); } public function process($week_days) { if($this->n_diff_days) { for($i = 0; $i <= $this->n_diff_days; $i++) { if($i == 0) { $date = $this->start_date->format($this->format); $week_day = strtolower($this->start_date->format('D')); } else { $new_date = $this->start_date->modify('+1 day'); $date = $new_date->format($this->format); $week_day = strtolower($new_date->format('D')); } if(in_array($this->week_days[$week_day], $week_days)) { $this->result[] = ['date' => $date, 'day' => $this->week_days[$week_day]]; } } } return $this; } public function get() { return $this->result; } } $intervalDate = new IntervalDate('2014-10-01', '2014-10-20'); $week_days = ['monday', 'sunday']; $result = $intervalDate->process($week_days)->get(); var_dump($result);
Output for 5.4.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(6) { [0]=> array(2) { ["date"]=> string(10) "2014-10-05" ["day"]=> string(6) "sunday" } [1]=> array(2) { ["date"]=> string(10) "2014-10-06" ["day"]=> string(6) "monday" } [2]=> array(2) { ["date"]=> string(10) "2014-10-12" ["day"]=> string(6) "sunday" } [3]=> array(2) { ["date"]=> string(10) "2014-10-13" ["day"]=> string(6) "monday" } [4]=> array(2) { ["date"]=> string(10) "2014-10-19" ["day"]=> string(6) "sunday" } [5]=> array(2) { ["date"]=> string(10) "2014-10-20" ["day"]=> string(6) "monday" } }
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/HeWbt on line 12
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/HeWbt on line 12
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/HeWbt on line 12
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_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/HeWbt on line 12
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/HeWbt on line 12
Process exited with code 255.