3v4l.org

run code in 300+ PHP versions simultaneously
<?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>';
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/NMror
function name:  (null)
number of ops:  16
compiled vars:  !0 = $Tokyo, !1 = $now
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   NEW                                              $2      'DateTimeZone'
          1        SEND_VAL_EX                                              'Asia%2FTokyo'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $2
    3     4        NEW                                              $5      'DateTime'
          5        SEND_VAL_EX                                              'now'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !1, $5
    4     9        INIT_METHOD_CALL                                         !1, 'format'
         10        SEND_VAL_EX                                              'g%3Ai+A'
         11        DO_FCALL                                      0  $8      
         12        CONCAT                                           ~9      '%5C%3Cp%3EIn+Tokyo%2C+it%27s+', $8
         13        CONCAT                                           ~10     ~9, '%5C%3C%2Fp%3E'
         14        ECHO                                                     ~10
         15      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.97 ms | 1384 KiB | 13 Q