|
The Time\Duration classIntroductionRepresents a fixed length of elapsed time, such as a timeout or the time measured by a stopwatch, with nanosecond precision. A duration consists of a non-negative number of whole Time\Duration::$seconds, a number of Time\Duration::$nanoseconds below one second, and a sign stored in Time\Duration::$negative.
Unlike DateInterval, a
Time\Duration does not have calendar units such as
days, months or years, because those do not map to a fixed number of
seconds: a day can be 23 or 25 hours long across a DST
transition, and months have different lengths.
The largest unit a duration can be created from is therefore the hour.
Use DateInterval for calendar arithmetic on
DateTimeImmutable objects, and
Time\Duration for unambiguous amounts of time,
for example the
Objects of this class are immutable.
They are created with one of the
Two durations can be compared with the
comparison operators
(
The maximum length of a duration is Class synopsisTime
final
readonly
Duration
/* Properties */
public
readonly
int
$seconds;
public
readonly
int
$nanoseconds;
public
readonly
bool
$negative;
/* Methods */
private Time\Duration::__construct()
public Time\Duration Time\Duration::absolute()
public Time\Duration Time\Duration::add(Time\Duration
$duration)public static int Time\Duration::compare(Time\Duration
$a, Time\Duration $b)public Time\Duration Time\Duration::divideBy(int
$divisor)public static Time\Duration Time\Duration::fromHours(int
$hours)public static Time\Duration Time\Duration::fromIso8601DurationString(string
$specification)public static Time\Duration Time\Duration::fromMicroseconds(int
$microseconds)public static Time\Duration Time\Duration::fromMilliseconds(int
$milliseconds)public static Time\Duration Time\Duration::fromMinutes(int
$minutes)public static Time\Duration Time\Duration::fromNanoseconds(int
$nanoseconds)public static Time\Duration Time\Duration::fromSeconds(int
$seconds, int $nanoseconds = 0)public Time\Duration Time\Duration::multiplyBy(int
$factor)public Time\Duration Time\Duration::negate()
public Time\Duration Time\Duration::sub(Time\Duration
$duration)public void Time\Duration::__unserialize(array
$data)Properties
ExamplesExample #1 Creating and combining durations The above example will output:
object(Time\Duration)#3 (3) {
["seconds"]=>
int(61)
["nanoseconds"]=>
int(500000000)
["negative"]=>
bool(false)
}
61.500000000 seconds
Example #2 Comparing durations The above example will output: 0.250000000 5.000000000 120.000000000 bool(true) bool(true) See Also
|