<?php
date_default_timezone_set('America/New_York');
$initialDate = DateTime::createFromFormat('U', '1409611999');
$initialDate = new DateTimeExtended($initialDate, new DateTimezone('UTC'));
$firstDayofWeek = clone $initialDate;
$lastDayofWeek = clone $initialDate;
$firstDayofWeek->setDayOfWeek('Monday', DateTimeExtended::WEEK_CURRENT);
$lastDayofWeek->setDayOfWeek('Sunday', DateTimeExtended::WEEK_CURRENT);
var_dump($firstDayofWeek, $lastDayofWeek);
class DateTimeExtended Extends DateTime {
const WEEK_CURRENT = 0;
const WEEK_NEXT = 1;
const WEEK_PREVIOUS = 2;
protected static $days_of_week = array('sunday',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
);
public function __construct()
{
$args = func_get_args();
if (empty($args)) {
throw new Exception('No argument supplied!');
}
if ($args[0] instanceof DateTime) {
parent::__construct($args[0]->format('Y-m-d H:i:s'), $args[0]->getTimezone());
} else {
parent::__construct($args[0], isset($arg[1]) ? $arg[1] : null);
}
}
public function setDayOfWeek($day, $margin = 0)
{
$day = strtolower($day);
if (!is_int($day)) {
$day = array_search($day, static::$days_of_week);
}
$current_day = $this->format('w');
switch($margin) {
case static::WEEK_CURRENT:
$interval = $day - $current_day;
break;
case static::WEEK_NEXT:
$interval = 1;
while($interval <= 7) {
$current_day = ($current_day + 1) % 7;
if ($current_day == $day) {
break;
}
$interval++;
}
break;
case static::WEEK_PREVIOUS:
$interval = -1;
while($interval >= -7) {
$current_day -= 1;
if ($current_day < 0) {
$current_day = 6;
}
if ($current_day == $day) {
break;
}
$interval--;
}
break;
default:
throw new Exception('Invalid margin supplied!');
}
$i = abs($interval);
$days = new DateInterval("P{$i}D");
if ($interval < 0) {
$this->sub($days);
} else {
$this->add($days);
}
return $this;
}
}
- Output for 5.4.30 - 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.20, 7.2.6 - 7.2.33, 7.3.16 - 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
- object(DateTimeExtended)#1 (3) {
["date"]=>
string(26) "2014-09-01 22:53:19.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
object(DateTimeExtended)#3 (3) {
["date"]=>
string(26) "2014-08-31 22:53:19.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
- Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.29
- object(DateTimeExtended)#1 (3) {
["date"]=>
string(19) "2014-09-01 22:53:19"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
object(DateTimeExtended)#3 (3) {
["date"]=>
string(19) "2014-08-31 22:53:19"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
- Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
- Parse error: syntax error, unexpected T_STATIC in /in/7QeJQ on line 46
Process exited with code 255. - Output for 5.0.0 - 5.0.5
- Parse error: parse error, unexpected T_STATIC in /in/7QeJQ on line 46
Process exited with code 255. - Output for 4.4.2 - 4.4.9
- Parse error: syntax error, unexpected T_VARIABLE in /in/7QeJQ on line 6
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_VARIABLE in /in/7QeJQ on line 6
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/7QeJQ on line 6
Process exited with code 255.
preferences:
182.6 ms | 401 KiB | 310 Q