@ 2014-09-14T16:59:26Z <?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);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.8 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 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.1.6 , 5.2.0 - 5.2.17 , 5.3.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.3.11 , 4.4.0 - 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 . preferences:dark mode live preview
198.49 ms | 401 KiB | 356 Q