php8.5
Home/ Manual/ time / duration/ Time\Duration::negate

Time\Duration::negate

PHP function Edit on GitHub ✎

(PHP 8 >= 8.6.0)

Returns the duration with the opposite sign

Description

Time\Duration::negate(): Time\Duration

Returns a duration of the same length with the opposite sign: a positive duration becomes negative, and a negative duration becomes positive. Negating a duration of zero returns a duration of zero, which is never negative.

Parameters Parameters

Return Values

Returns a new Time\Duration object.

Examples

Time\Duration::negate example

php
<?php
$duration = \Time\Duration::fromSeconds(30);

var_dump($duration->negate()->negative);
var_dump($duration->negate()->negate()->negative);

// A zero duration has no sign.
var_dump(\Time\Duration::fromSeconds(0)->negate()->negative);

The above example will output:

output
bool(true)
bool(false)
bool(false)

See Also

  • Time\Duration::absolute
  • Time\Duration::sub

Source: reference/datetime/time/duration/negate.xml · from the official PHP manual (php/doc-en)