diff --git a/Conversions/ConvertTime.php b/Conversions/ConvertTime.php new file mode 100644 index 0000000..d68323f --- /dev/null +++ b/Conversions/ConvertTime.php @@ -0,0 +1,91 @@ + [ + 'minutes' => 1 / 60, + 'hours' => 1 / 3600, + 'days' => 1 / 86400, + 'weeks' => 1 / 604800, + 'months' => 1 / 2592000, + 'years' => 1 / 31536000 + ], + 'minutes' => [ + 'seconds' => 60, + 'hours' => 1 / 60, + 'days' => 1 / 1440, + 'weeks' => 1 / 10080, + 'months' => 1 / 43200, + 'years' => 1 / 525600 + ], + 'hours' => [ + 'seconds' => 3600, + 'minutes' => 60, + 'days' => 1 / 24, + 'weeks' => 1 / 168, + 'months' => 1 / 720, + 'years' => 1 / 8760 + ], + 'days' => [ + 'seconds' => 86400, + 'minutes' => 1440, + 'hours' => 24, + 'weeks' => 1 / 7, + 'months' => 1 / 30.4, + 'years' => 1 / 365.25 + ], + 'weeks' => [ + 'seconds' => 604800, + 'minutes' => 10080, + 'hours' => 168, + 'days' => 7, + 'months' => 1 / 4.3, + 'years' => 1 / 52.17 + ], + 'months' => [ + 'seconds' => 2592000, + 'minutes' => 43200, + 'hours' => 720, + 'days' => 30.4, + 'weeks' => 4.3, + 'years' => 1 / 12 + ], + 'years' => [ + 'seconds' => 31536000, + 'minutes' => 525600, + 'hours' => 8760, + 'days' => 365.25, + 'weeks' => 52.17, + 'months' => 12 + ] + ]; + + if (!isset($conversionFactors[$fromUnit], $conversionFactors[$fromUnit][$toUnit])) { + throw new InvalidArgumentException("Invalid time unit(s) specified: $fromUnit to $toUnit."); + } + + $conversionFactor = $conversionFactors[$fromUnit][$toUnit]; + $result = $value * $conversionFactor; + + return round($result, 2); +} + +// Example usage +try { + $timeResult = convertTime(120, 'minutes', 'seconds'); + echo "120 minutes = $timeResult seconds\n"; +} catch (InvalidArgumentException $e) { + echo "Error: " . $e->getMessage() . "\n"; +} diff --git a/DIRECTORY.md b/DIRECTORY.md index 30aa2c7..ecfa030 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -16,6 +16,7 @@ * [Octaltodecimal](./Conversions/OctalToDecimal.php) * [Speedconversion](./Conversions/SpeedConversion.php) * [Temperatureconversions](./Conversions/TemperatureConversions.php) + * [ConvertTime](./Conversions/ConvertTime.php) ## Datastructures * Avltree diff --git a/tests/Conversions/ConversionsTest.php b/tests/Conversions/ConversionsTest.php index 93893b4..3ba91d0 100644 --- a/tests/Conversions/ConversionsTest.php +++ b/tests/Conversions/ConversionsTest.php @@ -10,6 +10,7 @@ require_once __DIR__ . '/../../Conversions/HexadecimalToDecimal.php'; require_once __DIR__ . '/../../Conversions/SpeedConversion.php'; require_once __DIR__ . '/../../Conversions/TemperatureConversions.php'; +require_once __DIR__ . '/../../Conversions/ConvertTime.php'; class ConversionsTest extends TestCase { @@ -142,4 +143,23 @@ public function testFahrenheitToKelvin() $this->expectExceptionMessage('Temperature (Fahrenheit) must be a number'); FahrenheitToKelvin("non-numeric"); } + + public function testConvertTime() + { + $examples = [ + [120, 'minutes', 'seconds', 7200], + [48, 'hours', 'days', 2.0], + [14, 'days', 'weeks', 2.0], + [8.6, 'weeks', 'months', 2.0], + [24, 'months', 'years', 2.0], + [3600, 'seconds', 'hours', 1.0], + ]; + + foreach ($examples as [$value, $from, $to, $expected]) { + $result = convertTime($value, $from, $to); + $this->assertEquals($expected, $result, "Failed converting $value $from to $to"); + echo "Example: $value $from = $result $to\n"; + } + } + }