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>';

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0190.00316.75
8.3.50.0310.00721.21
8.3.40.0030.01418.99
8.3.30.0090.00619.04
8.3.20.0000.00820.31
8.3.10.0030.00623.36
8.3.00.0050.00319.38
8.2.180.0090.00616.63
8.2.170.0110.00422.96
8.2.160.0140.00020.53
8.2.150.0030.00624.18
8.2.140.0040.00424.66
8.2.130.0030.00526.16
8.2.120.0040.00419.82
8.2.110.0040.00720.86
8.2.100.0100.00317.91
8.2.90.0050.00318.09
8.2.80.0000.00817.97
8.2.70.0040.00417.88
8.2.60.0030.00517.93
8.2.50.0000.00818.07
8.2.40.0050.00519.94
8.2.30.0040.00418.12
8.2.20.0000.00717.91
8.2.10.0000.00718.01
8.2.00.0050.00217.92
8.1.280.0030.01025.92
8.1.270.0060.00322.02
8.1.260.0000.00826.35
8.1.250.0050.00328.09
8.1.240.0040.01122.71
8.1.230.0040.00821.00
8.1.220.0000.00817.74
8.1.210.0040.00718.77
8.1.200.0060.00317.48
8.1.190.0040.00417.34
8.1.180.0090.00018.10
8.1.170.0050.00318.77
8.1.160.0070.00021.99
8.1.150.0040.00418.98
8.1.140.0040.00417.65
8.1.130.0040.00417.88
8.1.120.0040.00417.63
8.1.110.0000.00817.64
8.1.100.0000.00717.69
8.1.90.0000.00817.64
8.1.80.0040.00417.65
8.1.70.0050.00317.63
8.1.60.0090.00017.77
8.1.50.0030.00617.68
8.1.40.0000.00817.61
8.1.30.0030.00617.73
8.1.20.0000.00717.83
8.1.10.0000.00717.69
8.1.00.0050.00517.66
8.0.300.0040.00419.90
8.0.290.0070.00016.88
8.0.280.0000.00718.42
8.0.270.0070.00317.33
8.0.260.0030.00317.27
8.0.250.0000.00817.19
8.0.240.0040.00417.19
8.0.230.0050.00317.15
8.0.220.0070.00317.04
8.0.210.0030.00317.10
8.0.200.0000.00717.18
8.0.190.0070.00417.11
8.0.180.0070.00017.01
8.0.170.0030.00617.17
8.0.160.0040.00417.05
8.0.150.0030.00616.92
8.0.140.0030.00316.89
8.0.130.0030.00313.55
8.0.120.0050.00316.95
8.0.110.0000.00817.02
8.0.100.0070.00016.87
8.0.90.0040.00417.16
8.0.80.0060.01017.13
8.0.70.0050.00317.09
8.0.60.0030.00516.97
8.0.50.0030.00517.09
8.0.30.0140.00617.28
8.0.20.0100.01417.41
8.0.10.0000.00717.18
8.0.00.0110.01316.83
7.4.330.0020.00215.00
7.4.320.0030.00316.65
7.4.300.0030.00316.60
7.4.290.0070.00016.69
7.4.280.0040.00416.48
7.4.270.0030.00316.68
7.4.260.0030.00616.46
7.4.250.0000.00716.59
7.4.240.0020.00616.58
7.4.230.0050.00316.74
7.4.220.0200.00016.71
7.4.210.0000.01416.70
7.4.200.0000.00716.50
7.4.160.0100.00716.71
7.4.150.0210.00317.40
7.4.140.0100.01417.86
7.4.130.0030.01416.60
7.4.120.0030.01416.75
7.4.110.0070.01016.58
7.4.100.0070.01416.67
7.4.90.0110.00616.59
7.4.80.0120.00619.39
7.4.70.0060.00916.70
7.4.60.0100.01316.67
7.4.50.0000.00916.50
7.4.40.0110.00716.48
7.4.30.0120.00916.63
7.4.00.0030.01315.21
7.3.330.0000.00513.05
7.3.320.0050.00013.26
7.3.310.0050.00216.14
7.3.300.0030.00316.32
7.3.290.0000.01416.49
7.3.280.0090.00616.33
7.3.270.0070.01017.40
7.3.260.0130.00316.48
7.3.250.0110.00516.54
7.3.240.0070.01816.47
7.3.230.0120.00916.39
7.3.210.0120.00316.55
7.3.200.0060.01319.39
7.3.190.0090.01216.37
7.3.180.0000.01516.64
7.3.170.0110.00616.35
7.3.160.0090.00616.61
7.3.10.0090.00616.93
7.3.00.0070.00716.48
7.2.330.0080.00816.75
7.2.320.0100.01416.91
7.2.310.0070.01016.90
7.2.300.0160.00016.52
7.2.290.0060.01216.57
7.2.130.0080.00816.92
7.2.120.0060.01217.14
7.2.110.0160.00316.87
7.2.100.0160.00616.98
7.2.90.0110.00717.01
7.2.80.0070.01116.75
7.2.70.0140.00717.07
7.2.60.0100.00716.92
7.2.50.0130.00717.14
7.2.40.0040.01316.96
7.2.30.0060.01216.89
7.2.20.0150.00617.11
7.2.10.0090.00616.81
7.2.00.0080.00618.32
7.1.250.0120.00615.76
7.1.240.0090.00315.86
7.1.230.0060.01215.91
7.1.220.0120.00615.63
7.1.210.0000.01115.73
7.1.200.0100.00715.93
7.1.190.0130.00615.81
7.1.180.0180.00315.89
7.1.170.0130.00715.74
7.1.160.0130.00615.96
7.1.150.0110.00715.48
7.1.140.0090.00915.89
7.1.130.0050.00815.84
7.1.120.0060.01015.50
7.1.110.0070.01015.59
7.1.100.0090.00517.10
7.1.90.0040.01116.03
7.1.80.0070.00415.86
7.1.70.0110.00616.49
7.1.60.0050.01517.53
7.1.50.0170.00716.37
7.1.40.0030.00615.87
7.1.30.0150.00415.63
7.1.20.0070.01415.77
7.1.10.0100.01015.94
7.1.00.0070.03819.23
7.0.330.0030.01115.21
7.0.320.0100.00715.59
7.0.310.0070.01115.48
7.0.300.0140.00415.16
7.0.290.0100.00715.34
7.0.280.0120.00615.41
7.0.270.0100.01015.51
7.0.260.0190.00015.31
7.0.250.0060.01015.29
7.0.240.0030.01215.70
7.0.230.0110.00715.41
7.0.220.0060.00815.25
7.0.210.0070.01015.27
7.0.200.0050.00716.21
7.0.190.0060.00915.51
7.0.180.0000.01515.68
7.0.170.0060.01215.40
7.0.160.0110.00415.43
7.0.150.0110.00415.14
7.0.140.0050.04018.68
7.0.130.0120.00615.46
7.0.120.0080.00515.61
7.0.110.0070.00715.11
7.0.100.0030.01015.16
7.0.90.0090.00615.51
7.0.80.0040.01215.24
7.0.70.0110.00315.16
7.0.60.0070.02117.67
7.0.50.0070.03816.52
7.0.40.0040.05016.72
7.0.30.0230.04016.91
7.0.20.0220.03316.83
7.0.10.0120.02016.56
7.0.00.0060.02416.75
5.6.380.0060.01014.56
5.6.370.0030.01314.14
5.6.360.0040.01414.37
5.6.350.0160.00314.36
5.6.340.0060.00914.13
5.6.330.0110.00314.84
5.6.320.0030.00914.52
5.6.310.0000.01714.72
5.6.300.0200.00014.00
5.6.290.0110.00714.32
5.6.280.0080.03817.65
5.6.270.0070.01014.50
5.6.260.0090.00614.36
5.6.250.0110.00414.21
5.6.240.0030.01214.08
5.6.230.0050.00514.48
5.6.220.0030.01014.69
5.6.210.0120.04017.48
5.6.200.0030.02516.25
5.6.190.0070.02217.48
5.6.180.0160.04717.52
5.6.170.0580.02417.52
5.6.160.0090.02717.29
5.6.150.0070.04316.44
5.6.140.0080.04316.18
5.6.130.0020.03416.23
5.6.120.0130.04217.61
5.6.110.0140.02917.73
5.6.100.0070.04317.81
5.6.90.0050.04517.52
5.6.80.0170.03717.34
5.6.70.0080.00514.43
5.6.60.0060.00614.00
5.6.50.0080.00314.34
5.6.40.0050.00814.44
5.6.30.0100.00314.37
5.6.20.0100.00314.37
5.6.10.0090.00614.23
5.6.00.0070.01014.23
5.5.380.0080.00411.22
5.5.370.0060.00611.33
5.5.360.0000.01310.73
5.5.350.0080.02115.81
5.5.340.0020.03414.68
5.5.330.0070.04015.70
5.5.320.0290.03315.71
5.5.310.0130.03815.73
5.5.300.0070.02314.52
5.5.290.0020.03614.29
5.5.280.0070.02515.92
5.5.270.0050.03215.91
5.5.260.0100.04416.08
5.5.250.0030.02315.75
5.5.240.0130.04015.52
5.5.230.0040.00410.91
5.5.220.0060.00611.30
5.5.210.0030.00311.01
5.5.200.0030.00311.16
5.5.190.0030.00611.19
5.5.180.0000.01211.00
5.5.170.0030.00711.23
5.5.160.0040.00711.30
5.5.150.0040.00811.27
5.5.140.0030.01010.76
5.5.130.0040.00711.14
5.5.120.0030.00611.39
5.5.110.0060.00311.29
5.5.100.0040.00411.39
5.5.90.0060.00310.61
5.5.80.0060.00611.15
5.5.70.0060.00611.08
5.5.60.0030.00911.33
5.5.50.0080.00311.17
5.5.40.0080.00411.29
5.5.30.0030.00611.11
5.5.20.0040.00711.07
5.5.10.0070.00310.91
5.5.00.0070.00311.00
5.4.450.0460.02515.25
5.4.440.0510.03015.28
5.4.430.0580.03315.33
5.4.420.0350.00615.30
5.4.410.0370.00215.25
5.4.400.0330.00215.03
5.4.390.0320.00515.08
5.4.380.0370.00015.04
5.4.370.0450.00315.18
5.4.360.0320.00614.92
5.4.350.0370.00515.11
5.4.340.0100.01611.37
5.4.330.0070.00210.78
5.4.320.0070.02011.58
5.4.310.0070.02111.66
5.4.300.0080.01611.68
5.4.290.0030.02311.71
5.4.280.0020.02311.68
5.4.270.0030.02411.71
5.4.260.0030.02411.65
5.4.250.0040.02311.54
5.4.240.0060.01911.80
5.4.230.0080.02011.66
5.4.220.0050.02211.72
5.4.210.0040.02011.68
5.4.200.0030.02311.67
5.4.190.0070.01911.55
5.4.180.0040.02111.64
5.4.170.0050.01911.62
5.4.160.0040.02111.68
5.4.150.0070.02011.74
5.4.140.0070.02011.43
5.4.130.0060.02211.60
5.4.120.0060.02011.47
5.4.110.0070.01911.51
5.4.100.0050.02111.54
5.4.90.0030.02411.53
5.4.80.0020.02611.38
5.4.70.0040.02111.54
5.4.60.0040.02011.35
5.4.50.0050.02111.48
5.4.40.0080.01811.55
5.4.30.0110.01811.33
5.4.20.0090.01811.44
5.4.10.0080.02311.46
5.4.00.0040.02311.15
5.3.290.0050.02211.80
5.3.280.0050.02311.68
5.3.270.0040.02811.75
5.3.260.0050.02411.73
5.3.250.0040.02211.58
5.3.240.0030.02411.67
5.3.230.0060.02211.59
5.3.220.0080.02111.55
5.3.210.0060.02211.57
5.3.200.0040.02211.52
5.3.190.0060.02111.61
5.3.180.0040.02311.72
5.3.170.0070.02411.66
5.3.160.0060.02211.69
5.3.150.0050.02311.69
5.3.140.0040.02211.61
5.3.130.0040.02711.62
5.3.120.0070.02411.61
5.3.110.0030.02411.52
5.3.100.0040.02711.27
5.3.90.0050.02211.34
5.3.80.0050.02111.28
5.3.70.0060.02211.28
5.3.60.0050.02111.36
5.3.50.0080.02211.29
5.3.40.0040.02611.28
5.3.30.0060.02011.23
5.3.20.0060.02111.12
5.3.10.0020.02611.00
5.3.00.0050.02211.01
5.2.170.0040.0209.46
5.2.160.0070.0159.43
5.2.150.0040.0189.35
5.2.140.0050.0179.42
5.2.130.0030.0189.33
5.2.120.0030.0189.36
5.2.110.0050.0159.29
5.2.100.0020.0229.35
5.2.90.0070.0159.33
5.2.80.0070.0189.17
5.2.70.0040.0189.29
5.2.60.0020.0219.28
5.2.50.0020.0209.16
5.2.40.0060.0169.26
5.2.30.0040.0219.22
5.2.20.0060.0149.16
5.2.10.0070.0149.16
5.2.00.0030.0179.04
5.1.60.0020.0168.24
5.1.50.0070.0128.06
5.1.40.0030.0158.11
5.1.30.0030.0168.49
5.1.20.0060.0148.41
5.1.10.0060.0148.34
5.1.00.0070.0148.08
5.0.50.0020.0127.16
5.0.40.0020.0147.09
5.0.30.0030.0197.00
5.0.20.0040.0106.98
5.0.10.0040.0106.97
5.0.00.0020.0196.97
4.4.90.0010.0116.24
4.4.80.0020.0116.23
4.4.70.0050.0096.22
4.4.60.0030.0106.22
4.4.50.0020.0116.23
4.4.40.0010.0176.20
4.4.30.0020.0136.23
4.4.20.0040.0126.27
4.4.10.0040.0116.27
4.4.00.0030.0146.22
4.3.110.0040.0096.18
4.3.100.0040.0086.18
4.3.90.0020.0096.16
4.3.80.0020.0156.14
4.3.70.0020.0096.16
4.3.60.0030.0096.16
4.3.50.0020.0106.16
4.3.40.0010.0176.12
4.3.30.0010.0125.49
4.3.20.0020.0115.48
4.3.10.0010.0115.46
4.3.00.0050.0177.81

preferences:
44.28 ms | 401 KiB | 5 Q