3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Prorate { // source and destination prices will be expressed internally as price per day once set. private $source_price = 0; private $destination_price = 0; // source expiry is what you're pro-rating. destination expiry is what you're converting. private $source_expiry; private $destination_expiry; // today defaults to the current date, but can be set to an arbitrary value using Prorate::set_today() private $today; // terms are important! they allow us to calculate the current price per day private $source_term = 12; private $destination_term = 12; // source price is what you're pro-rating. Destination_price is what you're converting. Source and destination terms // follow the same rules. public function __construct($source_price = NULL, $destination_price = NULL, $source_term = 12, $destination_term = 12) { if ($source_price != NULL) { $this->set_source_price($source_price, $source_term); } if ($destination_price != NULL) { $this->set_destination_price($destination_price, $destination_term); } $this->source_expiry = new DateTime(); $this->destination_expiry = new DateTime(); $this->today = new DateTime(); $this->source_term = $source_term; $this->destination_term = $destination_term; } // set the source expiry date. Valid inputs are either a DateTime object, or a string format listed at // http://php.net/manual/en/datetime.formats.php public function set_source_expiry($date) { if(is_object($date) && is_a($date, 'DateTime')) { $this->source_expiry = $date; } else { $this->source_expiry = new DateTime($date); } return $this; } public function get_source_expiry() { return $this->source_expiry; } // set the destination expiry date. Valid inputs are either a DateTime object, or a string format listed at // http://php.net/manual/en/datetime.formats.php public function set_destination_expiry($date) { if(is_object($date) && is_a($date, 'DateTime')) { $this->destination_expiry = $date; } else { $this->destination_expiry = new DateTime($date); } return $this; } public function get_destination_expiry() { return $this->destination_expiry; } // returns the number of (positive) days remaining between a source date in the future, and today private function days_remaining($date) { return $this->diff_days($date, $this->today); } // determines the daily value of a given price over a given number of months. // 30.44 is 365 days / 12 months private function value_per_day($price_in_cents, $term_in_months) { $value_per_day = $price_in_cents / $term_in_months / 30.44; return ceil($value_per_day) / 100; } // Allows you to change what "today" is defined as. Defaults to the current date. Valid values are // a DateTime object, or a string representation of a date in one of the formats listed at: // http://php.net/manual/en/datetime.formats.php public function set_today($date = NULL) { if(is_object($date) && is_a($date, 'DateTime')) { $this->today = $date; } else { if (is_null($date)) { $this->today = new DateTime(); } else { $this->today = new DateTime($date); } } } // sets the source price (the thing you're pro-rating) // valid inputs are a price in cents, and a term expressed in months public function set_source_price($price_in_cents, $term_in_months) { $this->source_price = $this->value_per_day($price_in_cents, $term_in_months); return $this; } public function get_source_price() { return $this->source_price; } // sets the target price (the thing you're converting) // valid inputs are a price in cents, and a term expressed in months public function set_destination_price($price_in_cents, $term_in_months) { $this->destination_price = $this->value_per_day($price_in_cents, $term_in_months); return $this; } public function get_destination_price() { return $this->destination_price; } // calculates the target value based on the current calculated price per day and time // remaining on expiry public function target_value() { return $this->days_remaining($this->destination_expiry) * $this->destination_price; } // calculates the number of days to pro-rate. this does not include the number of days // remaining on the source. public function add_days() { return ceil($this->target_value() / $this->source_price); } // DateTime diff implementation for systems that are not new enough for PHP's regular DateTime::diff() method // if there is a partial day, it is rounded up to count as a full day. private function diff_days(DateTime $x, DateTime $y) { $y = strtotime($y->format('Y-m-d')); $x = strtotime($x->format('Y-m-d')); $diff = $x - $y; if ($diff <= 0) return 0; return ceil($diff / 86400); } // Call this once all your values are set. Returns an associative array containing the number of days // remaining on the source, the number of days to add to the source, the total number of days (source + add) // and a DateTime object representing the value defined as "today" plus the total days to add. public function pro_rate() { $days = $this->diff_days($this->source_expiry, $this->today); $result = array(); $add = $this->add_days(); $total_add = $days + $add; $today = $this->today->format('Y-m-d'); $source = $this->source_expiry->format('Y-m-d'); $dest = $this->destination_expiry->format('Y-m-d'); $result['current_days'] = $days; $result['pro_rate_days'] = $add; $result['total_days'] = $total_add; $tmp = clone $this->today; $result['date'] = $tmp->modify("+{$result['total_days']} days"); return $result; } } $prorate = new Prorate(19990, 19995); $prorate->set_source_expiry('2014-07-23'); $prorate->set_destination_expiry('19-01-2014'); $result = $prorate->pro_rate(); var_dump($result);

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.0110.00716.88
8.3.50.0090.01017.76
8.3.40.0040.01219.27
8.3.30.0000.01618.92
8.3.20.0030.00519.02
8.3.10.0030.00520.71
8.3.00.0080.00017.84
8.2.180.0100.01017.00
8.2.170.0080.00822.96
8.2.160.0110.00722.26
8.2.150.0040.00424.18
8.2.140.0060.00324.66
8.2.130.0040.00426.16
8.2.120.0080.00020.14
8.2.110.0030.00622.39
8.2.100.0110.00417.97
8.2.90.0030.00518.13
8.2.80.0070.00317.97
8.2.70.0040.00418.18
8.2.60.0060.00318.41
8.2.50.0000.00818.10
8.2.40.0040.00419.73
8.2.30.0070.00019.64
8.2.20.0040.00418.09
8.2.10.0070.00317.96
8.2.00.0030.00618.19
8.1.280.0130.01025.92
8.1.270.0040.00424.66
8.1.260.0080.00026.35
8.1.250.0000.00828.09
8.1.240.0060.00324.04
8.1.230.0030.00919.20
8.1.220.0050.00318.14
8.1.210.0000.00818.77
8.1.200.0040.00417.86
8.1.190.0000.00917.72
8.1.180.0030.00618.10
8.1.170.0060.00319.02
8.1.160.0070.00019.02
8.1.150.0040.00419.07
8.1.140.0040.00417.73
8.1.130.0000.00817.71
8.1.120.0000.00717.72
8.1.110.0050.00317.82
8.1.100.0050.00317.84
8.1.90.0040.00417.88
8.1.80.0000.00717.81
8.1.70.0040.00417.75
8.1.60.0040.00417.91
8.1.50.0040.00417.69
8.1.40.0030.00517.86
8.1.30.0030.00517.94
8.1.20.0030.00617.89
8.1.10.0000.00817.96
8.1.00.0040.00417.89
8.0.300.0040.00419.03
8.0.290.0070.00417.25
8.0.280.0000.00818.71
8.0.270.0030.00517.53
8.0.260.0000.00717.22
8.0.250.0000.00817.29
8.0.240.0050.00617.42
8.0.230.0000.00817.43
8.0.220.0000.00717.23
8.0.210.0040.00417.37
8.0.200.0030.00317.32
8.0.190.0000.01017.41
8.0.180.0070.00017.29
8.0.170.0000.01017.39
8.0.160.0000.00717.21
8.0.150.0040.00417.16
8.0.140.0060.00617.35
8.0.130.0030.00313.75
8.0.120.0040.00417.32
8.0.110.0030.00517.36
8.0.100.0000.00817.20
8.0.90.0000.00817.39
8.0.80.0060.01617.40
8.0.70.0040.00417.24
8.0.60.0030.00617.25
8.0.50.0030.00617.27
8.0.30.0100.01317.54
8.0.20.0080.01117.59
8.0.10.0030.00617.30
8.0.00.0110.00917.26
7.4.330.0000.00513.31
7.4.320.0000.00716.86
7.4.300.0030.00316.83
7.4.290.0040.00416.94
7.4.280.0030.00816.87
7.4.270.0030.00516.82
7.4.260.0000.00513.54
7.4.250.0030.00616.91
7.4.240.0040.00416.89
7.4.230.0070.00016.93
7.4.220.0130.00316.83
7.4.210.0080.00716.86
7.4.200.0040.00416.78
7.4.190.0000.00716.91
7.4.160.0160.00616.82
7.4.150.0120.01216.86
7.4.140.0080.01016.84
7.4.130.0110.00816.85
7.4.120.0060.01216.98
7.4.110.0070.01016.89
7.4.100.0070.01416.98
7.4.90.0120.00916.79
7.4.80.0060.01216.82
7.4.70.0050.01116.87
7.4.60.0040.01316.91
7.4.50.0000.00616.89
7.4.40.0060.00917.03
7.4.30.0100.00917.12
7.4.00.0030.01015.08
7.3.330.0000.00613.62
7.3.320.0050.00013.68
7.3.310.0030.00616.57
7.3.300.0000.00716.53
7.3.290.0060.01316.69
7.3.280.0080.01116.66
7.3.270.0100.00716.91
7.3.260.0170.00316.89
7.3.250.0160.01016.82
7.3.240.0120.00616.94
7.3.230.0090.00916.96
7.3.210.0170.00016.98
7.3.200.0060.01219.39
7.3.190.0090.01216.88
7.3.180.0090.01416.80
7.3.170.0080.00816.90
7.3.160.0150.00316.77
7.3.120.0110.00715.19
7.3.10.0120.00716.80
7.3.00.0090.00616.54
7.2.330.0070.01117.14
7.2.320.0220.00317.19
7.2.310.0120.00916.85
7.2.300.0090.00916.89
7.2.290.0060.01217.12
7.2.130.0080.00516.86
7.2.120.0060.01017.00
7.2.110.0200.00017.00
7.2.100.0030.01016.54
7.2.90.0090.00916.63
7.2.80.0140.00716.82
7.2.70.0000.01216.81
7.2.60.0040.01117.17
7.2.50.0190.00216.61
7.2.40.0130.00716.94
7.2.30.0190.00316.70
7.2.20.0130.00917.19
7.2.10.0120.00816.64
7.2.00.0060.00818.37
7.1.250.0170.00315.64
7.1.200.0060.00715.84
7.1.100.0100.00317.96
7.1.70.0090.00017.09
7.1.60.0030.01619.40
7.1.50.0040.00817.27
7.1.00.0000.05722.31
7.0.200.0000.00816.71
7.0.140.0000.07722.01
7.0.120.0030.07322.06
7.0.110.0100.08319.99
7.0.100.0170.07020.16
7.0.90.0230.08020.13
7.0.80.0130.07319.97
7.0.70.0030.08319.96
7.0.60.0100.08320.12
7.0.50.0130.08320.02
7.0.40.0100.04319.81
7.0.30.0100.07019.75
7.0.20.0070.05719.70
7.0.10.0070.08019.78
7.0.00.0130.04019.73
5.6.280.0100.06721.16
5.6.260.0000.05320.63
5.6.250.0070.04720.73
5.6.240.0100.07720.73
5.6.230.0070.07020.72
5.6.220.0030.06020.88
5.6.210.0070.07320.85
5.6.200.0130.07720.59
5.6.190.0030.05020.59
5.6.180.0070.06720.73
5.6.170.0130.07720.73
5.6.160.0000.08320.50
5.6.150.0070.07320.68
5.6.140.0130.08320.74
5.6.130.0230.06720.73
5.6.120.0070.04320.73
5.6.110.0030.07720.82
5.6.100.0130.08720.64
5.6.90.0070.03720.55
5.6.80.0030.04320.08
5.6.70.0070.03720.11
5.6.60.0030.08320.11
5.6.50.0000.08320.04
5.6.40.0070.05020.04
5.6.30.0070.04019.95
5.6.20.0030.09319.97
5.6.10.0070.04720.01
5.6.00.0000.04320.10
5.5.380.0130.03717.62
5.5.370.0070.08017.62
5.5.360.0170.05317.56
5.5.350.0100.06717.52
5.5.340.0100.08318.05
5.5.330.0000.04718.12
5.5.320.0070.07317.95
5.5.310.0030.08018.08
5.5.300.0000.07317.92
5.5.290.0130.09017.97
5.5.280.0030.04017.85
5.5.270.0030.03718.13
5.5.260.0070.07318.07
5.5.250.0000.04017.86
5.5.240.0130.06717.47
5.5.230.0070.06717.61
5.5.220.0100.06317.35
5.5.210.0030.07017.32
5.5.200.0000.04017.59
5.5.190.0070.04717.33
5.5.180.0130.06717.48
5.5.160.0100.07017.28
5.5.150.0070.07717.49
5.5.140.0100.07317.50
5.5.130.0070.07317.46
5.5.120.0000.04717.30
5.5.110.0030.06717.19
5.5.100.0130.04017.20
5.5.90.0070.05317.32
5.5.80.0070.07717.38
5.5.70.0070.03717.37
5.5.60.0130.07017.09
5.5.50.0030.08017.16
5.5.40.0100.06317.47
5.5.30.0130.07317.46
5.5.20.0070.05317.31
5.5.10.0130.05317.31
5.5.00.0100.06017.16
5.4.450.0000.08719.50
5.4.440.0000.06019.50
5.4.430.0030.03719.51
5.4.420.0170.06719.35
5.4.410.0130.06719.21
5.4.400.0030.06319.18
5.4.390.0070.03319.16
5.4.380.0000.08319.00
5.4.370.0030.07718.95
5.4.360.0100.03319.11
5.4.350.0070.06319.09
5.4.340.0200.02719.01
5.4.320.0070.05719.33
5.4.310.0070.07319.01
5.4.300.0130.06719.15
5.4.290.0030.05019.02
5.4.280.0130.05719.17
5.4.270.0030.04018.98
5.4.260.0070.04019.19
5.4.250.0100.03019.00
5.4.240.0070.07319.00
5.4.230.0030.09719.18
5.4.220.0100.04019.15
5.4.210.0100.05319.27
5.4.200.0070.05319.23
5.4.190.0100.03319.17
5.4.180.0170.06318.99
5.4.170.0070.04319.33
5.4.160.0100.08018.96
5.4.150.0070.07719.21
5.4.140.0070.04716.55
5.4.130.0070.07316.58
5.4.120.0100.04716.38
5.4.110.0030.08016.59
5.4.100.0030.07016.63
5.4.90.0070.07316.65
5.4.80.0070.06716.54
5.4.70.0070.05716.58
5.4.60.0100.06716.41
5.4.50.0100.05016.56
5.4.40.0030.03716.64
5.4.30.0000.06016.57
5.4.20.0070.03316.29
5.4.10.0130.06716.43
5.4.00.0130.03316.06
5.3.290.0000.07014.69
5.3.280.0100.03714.64
5.3.270.0070.06014.67
5.3.260.0030.04714.73
5.3.250.0070.07314.66
5.3.240.0100.05714.75
5.3.230.0100.07014.63
5.3.220.0030.07714.64
5.3.210.0000.06714.59
5.3.200.0100.06314.60
5.3.190.0070.07314.64
5.3.180.0030.08014.63
5.3.170.0070.04314.59
5.3.160.0100.07014.61
5.3.150.0100.04714.58
5.3.140.0000.04714.45
5.3.130.0070.07714.72
5.3.120.0100.06314.55
5.3.110.0100.07714.73
5.3.100.0030.05014.22
5.3.90.0000.05314.15
5.3.80.0030.05014.08
5.3.70.0070.07714.05
5.3.60.0030.08013.90
5.3.50.0130.06013.95
5.3.40.0030.07714.02
5.3.30.0030.04313.96
5.3.20.0030.04313.86
5.3.10.0100.08313.92
5.3.00.0100.07013.71
5.2.170.0000.04712.30
5.2.160.0070.04312.30
5.2.150.0030.05712.30
5.2.140.0070.06012.30
5.2.130.0070.03312.30
5.2.120.0070.02712.30
5.2.110.0000.06712.30
5.2.100.0070.05712.30
5.2.90.0030.06012.30
5.2.80.0070.05312.30
5.2.70.0130.02012.30
5.2.60.0070.05712.30
5.2.50.0030.05312.30
5.2.40.0030.06012.30
5.2.30.0030.04012.30
5.2.20.0070.05312.30
5.2.10.0030.05312.30
5.2.00.0070.06312.30
5.1.60.0030.03312.30
5.1.50.0000.05712.30
5.1.40.0100.03312.30
5.1.30.0070.04312.30
5.1.20.0030.05712.30
5.1.10.0070.05312.30
5.1.00.0070.03012.30
5.0.50.0030.03712.30
5.0.40.0030.04712.30
5.0.30.0000.07012.30
5.0.20.0030.03012.30
5.0.10.0100.01712.30
5.0.00.0100.05012.30
4.4.90.0030.01712.30
4.4.80.0030.03012.30
4.4.70.0030.03312.30
4.4.60.0100.02712.30
4.4.50.0070.02712.30
4.4.40.0000.03712.30
4.4.30.0000.03712.30
4.4.20.0070.03012.30
4.4.10.0000.03312.30
4.4.00.0030.05012.30
4.3.110.0070.01712.30
4.3.100.0000.03312.30
4.3.90.0000.03312.30
4.3.80.0030.03312.30
4.3.70.0000.03312.30
4.3.60.0030.02312.30
4.3.50.0070.02312.30
4.3.40.0030.04712.30
4.3.30.0030.03012.30
4.3.20.0000.02312.30
4.3.10.0070.01012.30
4.3.00.0030.03312.30

preferences:
42.25 ms | 401 KiB | 5 Q