/** * Convert the given time duration in the given unit to this * unit. Conversions from finer to coarser granularities * truncate, so lose precision. For example converting * 999 milliseconds to seconds results in * 0. Conversions from coarser to finer granularities * with arguments that would numerically overflow saturate to * "-PHP_INT_MAX -1" if negative or "PHP_INT_MAX" * if positive. * * For example, to convert 10 minutes to milliseconds, use: * TimeUnit::MILLISECONDS()->convert(10, TimeUnit::MINUTES); * * @param int $sourceDuration the time duration in the given source unit * @param TimeUnit $sourceUnit the unit of the source duration argument * @return float|int */ public function convert($sourceDuration, TimeUnit $sourceUnit) { switch ($this->getValue()) { case self::NANOSECONDS: return $sourceUnit->toNanos($sourceDuration); case self::MICROSECONDS: return $sourceUnit->toMicros($sourceDuration); case self::MILLISECONDS: return $sourceUnit->toMillis($sourceDuration); case self::SECONDS: return $sourceUnit->toSeconds($sourceDuration); case self::MINUTES: return $sourceUnit->toMinutes($sourceDuration); case self::HOURS: return $sourceUnit->toHours($sourceDuration); case self::DAYS: return $sourceUnit->toDays($sourceDuration); } }