3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php //This is the Date class from the second chapter of our book. The comments //are in the download version from the book. class Pos_Date extends DateTime{protected $_year;protected $_month;protected $_day;public function __construct($timezone = null){ //call the parent constructor if ($timezone){ parent::__construct('now',$timezone); }else{ parent::__construct('now'); } //assign the values to the class properties $this->_year = (int) $this->format('Y'); $this->_month = (int) $this->format('n'); $this->_day = (int) $this->format('j'); }public function setTime($hours, $minutes, $seconds = 0){ if (!is_numeric($hours) || !is_numeric($minutes) || !is_numeric($seconds)) { throw new Exception('setTime() expects two or three numbers sexparated by commas in the order: hours, minutes, seconds');}$outOfRange = false;if($hours < 0 || $hours > 23){ $outOfRange = true;}if ($minutes < 0 || $minutes >59) { $outOfRange = true;}if ($seconds < 0 || $seconds > 59){ $outOfRange = true;}if ($outOfRange) { throw new Exception('Invalid time.');}parent::setTime($hours, $minutes, $seconds);}public function setDate($year, $month, $day){ if (!is_numeric($year) || !is_numeric($month) || !is_numeric($day)){ throw new Exception('setDate() expects three numbers separated by commas in the order: year, month, day.'); } if (!checkdate($month, $day, $year)){ throw new Exception('Non-existent date.');}parent::setDate($year, $month, $day);$this->_year = (int) $year;$this->_month = (int) $month;$this->_day = (int) $day;} // Adding many functions below to accomodate the different formats of dates that are possible // can remove any that are not needed. public function modify($modify) { throw new Exception('modify() has been disabled.');}public function setDMY($EuroDate){ $dateParts = preg_split('{[-/ :.]}', $EuroDate); if (!is_array($dateParts)|| count($dateParts) != 3){ throw new Exception('setDMY() expects a date as "DD/MM/YYYY".'); } $this->setDate($dateParts[2], $dateParts[1], $dateParts[0]);}public function setMDY($USDate){ $dateParts = preg_split('{[-/ :.]}', $USDate); if (!is_array($dateParts)|| count($dateParts) != 3){ throw new Exception('setMDY() expects a date as "MM/DD/YYYY".'); } $this->setDate($dateParts[2], $dateParts[0], $dateParts[1]);}public function setFromMySql($MySQLDate){ $dateParts = preg_split('{[-/ :.]}', $MySQLDate); if (!is_array($dateParts)|| count($dateParts) != 3){ throw new Exception('setFromMySQL() expects a date as "YYYY/MM/DD".'); } $this->setDate($dateParts[0], $dateParts[1], $dateParts[2]);}public function getMDY($leadingZeros = false){ if ($leadingZeros){ return $this->format('m/d/Y'); }else{ return $this->format('n/j/Y'); }}public function getDMY($leadingZeros = false){ if ($leadingZeros) { return $this->format('d/m/Y'); }else{ return $this->format('j/n/Y'); }}public function getMySQLFormat(){ return $this->format('Y-m-d');}public function getFullYear(){ return $this->_year;}public function getYear(){ return $this->format('y');}public function getMonth($leadingZero = false){ return $leadingZero ? $this->format('m') : $this->_month;}public function getMOnthName(){ return $this->format('F');}public function getMonthAbbr(){ return $this->format('M');}public function getDay($leadingZero = false){ return $leadingZero ? $this->format('d') : $this->_day;}public function getDayOrdinal(){ return $this->format('jS');}public function getDayName(){ return $this->format('1');}public function getDayAbbr(){ return $this->format('D');}public function addDays($numDays){ if (!is_numeric($numDays) || $numDays < 1){ throw new Exception('addDays() expects a positive integer.'); } parent::modify('+' . intval($numDays) . ' days');}public function subDays($numDays){ if (!is_numeric($numDays)){ throw new Exception('subDays() expects a positive integer.'); } parent::modify('-' . abs(intval($numDays)) . ' days');}public function addWeeks($numWeeks){ if (!is_numeric($numWeeks) || $numWeeks < 1){ throw new Exception('addWeeks() expects a positive integer.'); } parent::modify('+' . intval($numWeeks) . ' weeks');}public function subWeeks($numWeeks){ if (!is_numeric($numWeeks)){ throw new Exception('subWeeks() expects a positive integer.'); } parent::modify('-' . abs(intval($numWeeks)) . ' weeks');} public function addMonths($numMonths){ if (!is_numeric($numMonths) || $numMonths < 1){ throw new Exception('addMonths() expects a positive integer.'); } $numMonths = (int) $numMonths; //add the months to the current month number. $newValue = $this->_month + $numMonths; //if the new value is less than or equal to 12, the year //doesnøt change, so justassign ghe new value to the month. if ($newValue <= 12) { $this->_month = $newValue; }else{ //A new value greater than 12 means calculating both //the month and the year. Calculating the year is //different for DEcember, so do modulo division //by 12 on the new value. If the remainder is not 0, //the new month is not December. $notDecember =$newValue % 12; if ($notDecember) { //the remainder of the modulo division is the new month. $this->_month = notDecember; //Divide the new value by 12 and round down to get the //number of years to add. $this->_year += floor($newValue / 12); }else{ //The new month must be December $this->_month = 12; $this->_year += ($newValue / 12) -1; } } $this->checkLastDAyofMonth(); parent::setDate( $this->_year, $this->_month, $this->_day); } final protected function checkLastDayofMonth() { if(!checkdate($this->_month, $this->_day, $this->_year)) { $use30 = array(4, 6, 9, 11); if (in_array($this->_month, $use30)){ $this->_day = 30; }else{ $this->_day = $this->isLeap() ? 29 : 28; } } }public function isLeap(){ if ($this->_year % 400 == 0 || ($this->_year % 4 == 0 && $this->_year % 100 != 0)) { return true; }else{ return false; }}public function subMonths($numMonths){ if (!is_numeric($numMonths)){ throw new Exception('addMonths() expects an integer.'); } $numMonths = abs(intval($numMonths)); //Subtract the months from the current month number. $newValue = $this->_month - $numMonths; //if the result is greater than 0, itøs still the same year, //and you can assign the new value to the month. if($newValue > 0){ $this->_month = $newValue; }else{ //Create an arry of the months in reverse. $months = range(12 , 1); //Get the absolute value of $newValue. $newValue = abs($newValue); //Get the array position of the resulting month. $monthPosition =$newValue % 12; $this->_month =$months[$monthPosition]; //Arrays begin at 0, so if $monthPosition is 0, //it must be December. if ($monthPosition){ $this->_year -= ceil($newValue /12); }else{ $this->_year -= ceil($newVAlue /12) +1; } } $this->checkLastDayofMonth(); parent::setDate($this->_year, $this->_month, $this->_day);} public function addYears($numYears){ if (!is_numeric($numYears) || $numYears < 1){ throw new Exception('addYears() expects a positive integer.');}$this->_year += (int) $numYears;$this->checkLastDayOfMonth();parent::setDate($this->_year, $this->_month, $this->_day);}public function subYears($numYears){ if (!is_numeric($numYears)) { throw new Exception('subYears() expects an integer.'); } $this->_year -= abs(intVal($numYears)); $this->checkLastDayOfMonth(); parent::setDate($this->_year, $this->_month, $this->_day);} static public function dateDiff(Pos_Date $startDate, Pos_Date $endDate){ $start = gmmktime(0, 0, 0, $startDate->_month, $startDate->_day, $startDate->_year); $end =gmmktime(0, 0, 0, $endDate->_month, $endDate->_day, $endDate->_year); return ($end - $start) / (60 * 60 * 24);}public function dateDiff1(Pos_Date $endDate){ $start = gmmktime(0, 0, 0, $this->_month, $this->_day, $this->_year); $end =gmmktime(0, 0, 0, $endDate->_month, $endDate->_day, $endDate->_year); return ($end - $start) / (60 * 60 * 24);}public function __toString(){ return $this->format('l, F jS, Y'); }/** * Output date parts as read-only properties. * * Uses __get() magic method to create the following read-only * properties, all of which are case-insensitive: * * - MDY: date formatted as MM/DD/YYYY * - MDY0: date formatted as MM/DD/YYYY with leading zeros * - DMY: date formatted as DD/MM/YYYY * - DMY0: date formatted as DD/MM/YYYY with leading zeros * - MySQL: date formatted as YYYY-MM-DD * - fullYear: year as four digits * - year: year as two digits * - month: month number * - month0: month number with leading zero * - monthName: full name of month * - monthAbbr: month as 3-letter abbreviation * - day: day of month as number * - day0: day of month as number with leading zero * - dayOrdinal: day of month as ordinal number (1st, 2nd, etc.) * - dayName: full weekday name * - dayAbbr: weekday name as 3-letter abbreviation * * Any other value returns "Invalid property". * * @param string $name Name of read-only property. * @return string Formatted date or date part. */public function __get($name) { switch ( strtolower ( $name )) { case 'mdy' : return $this->format ( 'n/j/Y' ); case 'mdy0' : return $this->format ( 'm/d/Y' ); case 'dmy' : return $this->format ( 'j/n/Y' ); case 'dmy0' : return $this->format ( 'd/m/Y' ); case 'mysql' : return $this->format ( 'Y-m-d' ); case 'fullyear' : return $this->_year; case 'year' : return $this->format ( 'y' ); case 'month' : return $this->_month; case 'month0' : return $this->format ( 'm' ); case 'monthname' : return $this->format ( 'F' ); case 'monthabbr' : return $this->format ( 'M' ); case 'day' : return $this->_day; case 'day0' : return $this->format ( 'd' ); case 'dayordinal' : return $this->format ( 'jS' ); case 'dayname' : return $this->format ( 'l' ); case 'dayabbr' : return $this->format ( 'D' ); default : return 'Invalid property'; } }} $Tokyo = new DateTimeZone('Asia/Tokyo'); $now = new DateTime('now', $Tokyo); echo "\<p>In Tokyo, it's " . $now->format('g:i A') . '\</p>';
based on ImT8B
Output for 5.2.0 - 5.4.34, 5.6.28, 7.0.14 - 7.1.0
\<p>In Tokyo, it's 4:01 AM\</p>
Output for 7.0.6
\<p>In Tokyo, it's 5:35 PM\</p>
Output for 7.0.5
\<p>In Tokyo, it's 6:20 AM\</p>
Output for 7.0.4
\<p>In Tokyo, it's 5:57 PM\</p>
Output for 7.0.3
\<p>In Tokyo, it's 6:38 PM\</p>
Output for 7.0.2
\<p>In Tokyo, it's 12:36 AM\</p>
Output for 7.0.1
\<p>In Tokyo, it's 5:11 AM\</p>
Output for 7.0.0
\<p>In Tokyo, it's 6:43 AM\</p>
Output for 5.6.21
\<p>In Tokyo, it's 12:54 PM\</p>
Output for 5.6.20
\<p>In Tokyo, it's 5:20 AM\</p>
Output for 5.6.19
\<p>In Tokyo, it's 2:06 AM\</p>
Output for 5.6.18
\<p>In Tokyo, it's 10:12 AM\</p>
Output for 5.6.17
\<p>In Tokyo, it's 10:51 PM\</p>
Output for 5.6.16
\<p>In Tokyo, it's 7:40 AM\</p>
Output for 5.6.15
\<p>In Tokyo, it's 8:35 AM\</p>
Output for 5.6.14
\<p>In Tokyo, it's 10:54 AM\</p>
Output for 5.6.13
\<p>In Tokyo, it's 2:48 PM\</p>
Output for 5.6.12
\<p>In Tokyo, it's 4:37 AM\</p>
Output for 5.6.11
\<p>In Tokyo, it's 11:39 AM\</p>
Output for 5.6.10
\<p>In Tokyo, it's 1:24 PM\</p>
Output for 5.6.9
\<p>In Tokyo, it's 6:23 PM\</p>
Output for 5.6.8
\<p>In Tokyo, it's 2:32 AM\</p>
Output for 5.5.35
\<p>In Tokyo, it's 4:42 PM\</p>
Output for 5.5.34
\<p>In Tokyo, it's 7:41 AM\</p>
Output for 5.5.33
\<p>In Tokyo, it's 5:02 AM\</p>
Output for 5.5.32
\<p>In Tokyo, it's 11:45 AM\</p>
Output for 5.5.31
\<p>In Tokyo, it's 1:36 AM\</p>
Output for 5.5.30
\<p>In Tokyo, it's 10:32 AM\</p>
Output for 5.5.29
\<p>In Tokyo, it's 4:56 AM\</p>
Output for 5.5.28
\<p>In Tokyo, it's 7:54 AM\</p>
Output for 5.5.27
\<p>In Tokyo, it's 10:11 AM\</p>
Output for 5.5.26
\<p>In Tokyo, it's 3:51 PM\</p>
Output for 5.5.25
\<p>In Tokyo, it's 10:16 PM\</p>
Output for 5.5.24
\<p>In Tokyo, it's 5:30 AM\</p>
Output for 5.4.43 - 5.4.45
\<p>In Tokyo, it's 5:38 AM\</p>
Output for 5.4.35 - 5.4.42
\<p>In Tokyo, it's 12:41 AM\</p>
Output for 5.0.0 - 5.1.6
Fatal error: Class 'DateTimeZone' not found in /in/NMror on line 2
Process exited with code 255.
Output for 4.3.2 - 4.4.9
Fatal error: Cannot instantiate non-existent class: datetimezone in /in/NMror on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Cannot instantiate non-existent class: datetimezone in /in/NMror on line 2