You are viewing documentation for an outdated version. It is no longer supported!

Duration

Duration is a helper for dealing with relative time.

Example

use Aedart\Utils\Dates\Duration;

$a = new DateTime("2020-09-23 + 42 seconds + 23456 microseconds");
$b = new DateTime("2020-09-23 - 5 hours - 6 minutes");

$duration = Duration::fromDifference($a, $b);

echo $duration->format('%r%Y-%M-%D %H:%I:%S.%F'); //  '-00-00-00 05:06:42.023456'

Creating new instance

You can create a new Duration instance, using a variety of methods. The following demonstrates some of these:

$duration = Duration(42); // Using seconds
$duration = Duration(new DateInterval('P10Y7DT4H5M34S')); // Using DateInterval
$duration = Duration(new DateTime('@' . (42 * 60))); // Using DateTime

from()

The from() is a static alias for the Duration classes constructor.

$duration = Duration::from(new DateInterval('P10Y7DT4H5M34S'));

fromString()

fromString() can be used to create a new Duration instance, using a data and time string.

$duration = Duration::fromString('@' . (42 * 60));

fromSeconds()

As the name implies, fromSeconds() returns a new instance from specified amount of seconds.

$duration = Duration::fromSeconds(3600);

fromMinutes()

To create a new Instance from minutes, use fromMinutes().

$duration = Duration::fromMinutes(5);

fromHoursMinutes()

To create an instance from hours and optional minutes, use the fromHoursMinutes() method.

$duration = Duration::fromHoursMinutes(2, 30); // Minutes are optional

Should you have a hours and minutes string, like 02:30 or 1:25, then use the fromStringHoursMinutes() method to create duration instance.

$duration = Duration::fromStringHoursMinutes('02:25');

fromDifference()

When you wish to calculate the difference between two dates (or times), use the fromDifference().

$then = new DateTime("2020-09-23 - 5 hours - 6 minutes");
$when = new DateTime("2020-09-23 + 42 seconds + 23456 microseconds");

$duration = Duration::fromDifference($when, $then);

Convert

To obtain the duration in minutes, seconds or other format, use can use the following:

$duration = Duration::from(52200);

echo $duration->asSeconds(); // 52200
echo $duration->asMinutes(); // 870
echo $duration->toHoursMinutes(); // '14:30'
echo $duration->toHoursMinutes(true); // '14 hours 30 minutes'
echo $duration->toDaysHoursMinutes(); // '0-14:30'
echo $duration->toDaysHoursMinutes(true); // '0 days 14 hours 30 minutes'

Format

The format() can be used to format the duration into a string. Behind the scene, PHP native DateInterval::formatopen in new window is used.

$duration = Duration::from(new DateInterval('P10Y7DT4H5M34S'));

echo $duration->format('%Y-%M-%D %H:%I:%S'); // '10-00-07 04:05:34'

Onward

Please review the source code for additional methods and examples.