3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $initialDate = DateTime::createFromFormat('U', '1409611999'); $initialDate = new DateTimeExtended($initialDate); $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; } }
based on MCPOR
Output for 5.4.30 - 5.6.28, 7.0.0 - 7.1.0
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 hhvm-3.10.0 - 3.12.0
object(DateTimeExtended)#3 (3) { ["date"]=> string(26) "2014-09-01 22:53:19.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } object(DateTimeExtended)#4 (3) { ["date"]=> string(26) "2014-08-31 22:53:19.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 5.3.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.2.17
Parse error: syntax error, unexpected T_STATIC in /in/aqTVO on line 45
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STATIC in /in/aqTVO on line 45
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_VARIABLE in /in/aqTVO on line 5
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_VARIABLE in /in/aqTVO on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/aqTVO on line 5
Process exited with code 255.