3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Core\component\time; final class TimeFactory { /** @var int */ private $year; /** @var int */ private $weekNumber; /** @var int */ private $day; /** @var int */ private $hour; /** @var int */ private $minute; /** * @param string $formatted * @return static */ public static function createFromString(string $formatted): self { if (!preg_match('/^(\d{4}):(\d{2}):(\d{1}):(\d{2}):(\d{2})$/', $formatted, $matches)) { throw new \InvalidArgumentException('Invalid currency format.'); } return new self(...array_slice($matches, 1)); } /** * TimeFactory constructor. * @param int $year * @param int $weekNumber * @param int $day * @param int $hour * @param int $minute */ public function __construct(int $year, int $weekNumber, int $day, int $hour, int $minute) { $this->year = $year; $this->weekNumber = $weekNumber; $this->day = $day; $this->hour = $hour; $this->minute = $minute; } /** * @param int $addedYears * @return bool */ public function addYears(int $addedYears): bool { $newYears = $this->year + $addedYears; if($newYears > 9999) { return false; } else { $this->year += $newYears; } return true; } /** * @param int $addedWeeks */ public function addWeeks(int $addedWeeks): void { $newWeeks = $this->weekNumber + $addedWeeks; if($newWeeks >= 52) { $years = round($newWeeks / 52, 0, PHP_ROUND_HALF_DOWN); $this->addYears($years); } $this->weekNumber += $newWeeks % 52; } /** * @param int $addedDays */ public function addDays(int $addedDays): void { $newDays = $this->day + $addedDays; if($newDays >= 7) { $weeks = round($newDays / 7, 0, PHP_ROUND_HALF_DOWN); $this->addWeeks($weeks); } $this->day += $newDays % 7; } /** * @param int $addedHours */ public function addHours(int $addedHours): void { $newHours = $this->hour + $addedHours; if($newHours >= 24) { $days = round($newHours / 24, 0, PHP_ROUND_HALF_DOWN); $this->addDays($days); } $this->hour += $newHours % 24; } /** * @param int $addedMinutes */ public function addMinutes(int $addedMinutes): void { $newMinutes = $this->minute + $addedMinutes; if($newMinutes >= 60) { $hours = round($newMinutes / 60, 0, PHP_ROUND_HALF_DOWN); $this->addHours($hours); } $this->minute += $newMinutes % 60; } /** * @return int */ public function getYears(): int { return $this->year; } /** * @return int */ public function getWeeks(): int { return $this->weekNumber; } /** * @return int */ public function getDays(): int { return $this->day; } /** * @return int */ public function getHours(): int { return $this->hour; } /** * @return int */ public function getMinutes(): int { return $this->minute; } public function reduceTime(Int $amount, String $type) : bool { switch($type) { case "m": if($this->getMinutes() >= $amount) { $this->minute -= $amount; return true; } else { if($this->getHours() > 0) { $this->hour -= 1; $minutesToAdd = 60 - $amount; $this->addMinutes($minutesToAdd); return true; } elseif ($this->getDays() > 0) { $this->day -= 1; $hoursToAdd = 23; $minutesToAdd = 60 - $amount; $this->addHours($hoursToAdd); $this->addMinutes($minutesToAdd); return true; } elseif ($this->getWeeks() > 0) { $this->weekNumber -= 1; $daysToAdd = 6; $hoursToAdd = 23; $minutesToAdd = 60 - $amount; $this->addDays($daysToAdd); $this->addHours($hoursToAdd); $this->addMinutes($minutesToAdd); return true; } elseif ($this->getYears() > 0) { $this->year -= 1; $weeksToAdd = 51; $daysToAdd = 6; $hoursToAdd = 23; $minutesToAdd = 60 - $amount; $this->addWeeks($weeksToAdd); $this->addDays($daysToAdd); $this->addHours($hoursToAdd); $this->addMinutes($minutesToAdd); return true; } } break; case "h": if($this->getHours() >= $amount) { $this->hour -= $amount; return true; } else { if ($this->getDays() > 0) { $this->day -= 1; $hoursToAdd = 24 - $amount; $this->addHours($hoursToAdd); return true; } elseif ($this->getWeeks() > 0) { $this->weekNumber -= 1; $daysToAdd = 6; $hoursToAdd = 24 - $amount; $this->addDays($daysToAdd); $this->addHours($hoursToAdd); return true; } elseif ($this->getYears() > 0) { $this->year -= 1; $weeksToAdd = 51; $daysToAdd = 6; $hoursToAdd = 24 - $amount; $this->addWeeks($weeksToAdd); $this->addDays($daysToAdd); $this->addHours($hoursToAdd); return true; } } break; case "d": if($this->getDays() >= $amount) { $this->day -= $amount; return true; } else { if ($this->getWeeks() > 0) { $this->weekNumber--; $daysToAdd = 7 - $amount; $this->addDays($daysToAdd); return true; } elseif ($this->getYears() > 0) { $this->year--; $weeksToAdd = 51; $daysToAdd = 7 - $amount; $this->addWeeks($weeksToAdd); $this->addDays($daysToAdd); return true; } } break; case "w": if($this->getWeeks() >= $amount) { $this->weekNumber -= $amount; return true; } else { if ($this->getYears() > 0) { $weeksToAdd = 52 - $amount; $this->addWeeks($weeksToAdd); return true; } } break; case "y": if($this->getYears() >= $amount) { $this->year -= $amount; return true; } } return false; } /** * @return string */ public function __toString(): string { return sprintf('%04d:%02d:%d:%02d:%02d', $this->year, $this->weekNumber, $this->day, $this->hour, $this->minute); } } $factory = TimeFactory::createFromString("0000:00:0:00:00"); $factory->addMinutes(2); $factory->addMinutes(4); $factory->addMinutes(6); $factory->addMinutes(8); $factory->addMinutes(10); $factory->addMinutes(12); echo $factory; echo "\n" . $factory->getMinutes(); echo "\n" . $factory->getHours(); echo "\n" . $factory->getDays(); echo "\n" . $factory->getWeeks(); echo "\n" . $factory->getYears();

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.0120.00418.56
8.3.50.0060.01018.42
8.3.40.0160.00319.17
8.3.30.0130.00319.05
8.3.20.0060.00322.02
8.3.10.0050.00321.89
8.3.00.0180.00022.45
8.2.180.0100.01018.79
8.2.170.0100.01022.96
8.2.160.0090.00922.10
8.2.150.0080.00024.18
8.2.140.0050.00324.66
8.2.130.0040.00426.16
8.2.120.0040.00419.73
8.2.110.0000.00919.52
8.2.100.0100.00318.16
8.2.90.0060.00318.13
8.2.80.0000.00819.45
8.2.70.0000.00818.00
8.2.60.0030.00718.00
8.2.50.0030.00517.88
8.2.40.0030.00621.27
8.2.30.0040.00419.52
8.2.20.0050.00318.27
8.2.10.0080.00019.32
8.2.00.0050.00319.50
8.1.280.0140.00725.92
8.1.270.0000.00824.04
8.1.260.0030.00526.35
8.1.250.0040.00428.09
8.1.240.0090.01222.36
8.1.230.0090.00321.01
8.1.220.0070.00317.91
8.1.210.0030.00618.95
8.1.200.0060.00317.60
8.1.190.0030.00617.60
8.1.180.0040.00418.10
8.1.170.0000.00820.89
8.1.160.0080.00019.21
8.1.150.0030.00619.18
8.1.140.0000.00819.12
8.1.130.0000.00720.27
8.1.120.0070.00317.87
8.1.110.0030.00617.71
8.1.100.0040.00417.79
8.1.90.0040.00417.78
8.1.80.0030.00617.76
8.1.70.0030.00817.80
8.1.60.0040.00417.85
8.1.50.0030.00517.86
8.1.40.0000.00917.88
8.1.30.0040.00417.85
8.1.20.0000.00817.98
8.1.10.0040.00417.92
8.1.00.0060.00617.87
8.0.300.0040.00420.21
8.0.290.0000.00817.13
8.0.280.0040.00418.56
8.0.270.0040.00417.39
8.0.260.0040.00420.28
8.0.250.0000.00717.38
8.0.240.0040.00417.42
8.0.230.0050.00217.24
8.0.220.0030.00317.16
8.0.210.0050.00217.30
8.0.200.0040.00417.33
8.0.190.0000.00717.36
8.0.180.0080.00017.38
8.0.170.0040.00417.23
8.0.160.0040.00417.38
8.0.150.0030.00517.23
8.0.140.0000.00917.29
8.0.130.0070.00013.70
8.0.120.0060.00317.24
8.0.110.0040.00417.22
8.0.100.0050.00317.30
8.0.90.0040.00417.31
8.0.80.0070.01017.30
8.0.70.0050.00317.17
8.0.60.0030.00517.27
8.0.50.0000.00717.12
8.0.30.0150.00317.49
8.0.20.0200.00017.56
8.0.10.0050.00217.53
8.0.00.0070.01717.21
7.4.330.0050.00015.55
7.4.320.0000.00616.90
7.4.300.0000.00716.80
7.4.290.0080.00016.90
7.4.280.0030.00516.82
7.4.270.0000.00716.99
7.4.260.0030.00313.68
7.4.250.0080.00016.78
7.4.240.0040.00416.98
7.4.230.0000.00716.84
7.4.220.0080.00016.74
7.4.210.0040.01317.04
7.4.200.0040.00417.02
7.4.130.0090.01216.86
7.4.120.0150.00317.03
7.4.110.0030.01416.95
7.4.100.0060.01616.74
7.4.90.0100.01016.92
7.4.80.0140.00719.39
7.4.70.0040.01316.91
7.4.60.0040.01516.85
7.4.50.0090.00316.67
7.4.40.0130.00516.95
7.3.330.0030.00316.71
7.3.320.0000.00513.42
7.3.310.0080.00016.62
7.3.300.0030.00316.62
7.3.290.0040.00416.70
7.3.280.0120.00616.66
7.3.260.0120.00716.81
7.3.240.0120.00816.82
7.3.230.0100.00716.95
7.3.210.0060.01217.02
7.3.200.0070.01016.74
7.3.190.0140.00316.79
7.3.180.0000.02016.70
7.3.170.0140.00916.55
7.3.160.0070.01016.75
7.3.110.0110.00415.33
7.3.100.0070.00714.95
7.3.90.0000.01515.14
7.3.80.0090.00615.33
7.3.70.0030.01015.16
7.3.60.0060.00615.10
7.3.50.0110.00415.17
7.3.40.0000.01314.99
7.3.30.0080.00615.01
7.3.20.0110.00415.07
7.3.10.0070.00715.08
7.3.00.0150.00015.14
7.2.330.0090.00916.88
7.2.320.0140.00416.91
7.2.310.0130.00317.14
7.2.300.0120.01017.18
7.2.290.0160.00817.12
7.2.240.0120.00315.42
7.2.230.0030.01215.39
7.2.220.0060.00915.20
7.2.210.0040.01115.52
7.2.200.0030.01715.27
7.2.190.0030.01015.05
7.2.180.0040.01115.07
7.2.170.0070.00715.14
7.2.160.0100.00315.20
7.2.150.0070.02015.41
7.2.140.0070.00715.33
7.2.130.0070.00715.30
7.2.120.0120.00615.38
7.2.110.0030.01715.22
7.2.100.0100.01015.27
7.2.90.0080.01115.50
7.2.80.0090.00615.50
7.2.70.0070.00715.54
7.2.60.0030.01115.24
7.2.50.0130.00615.31
7.2.40.0120.00915.52
7.2.30.0120.01215.23
7.2.20.0040.01315.31
7.2.10.0000.01515.49
7.2.00.0110.00415.32
7.1.330.0080.00514.28
7.1.320.0090.00913.97
7.1.310.0060.00814.17
7.1.300.0000.01414.35
7.1.290.0030.01014.34
7.1.280.0030.00914.27
7.1.270.0070.00714.24
7.1.260.0070.00714.22
7.1.250.0110.00414.14

preferences:
80.03 ms | 400 KiB | 5 Q