2017 © Pedro Peláez
 

library ahora

Interval calculator to help sum up multiple time intervals and display the total elapsed days/hours/minutes/seconds

image

savvywombat/ahora

Interval calculator to help sum up multiple time intervals and display the total elapsed days/hours/minutes/seconds

  • Thursday, June 28, 2018
  • by SavvyWombat
  • Repository
  • 1 Watchers
  • 2 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Ahora

Time interval calculator., (*1)

Allows interval to be summed up and provides easy access the number of seconds/minutes/hours/days included in the interval., (*2)

Installation

Using Composer:, (*3)

composer require savvywombat/ahora

Usage

Creation

You can create a new interval as a new instance, optionally passing in an ISO8601 interval specification:, (*4)

use SavvyWombat\Ahora\Interval;

$interval = new Interval();
echo $interval->seconds; // outputs 0

$interval = new Interval("PT100D"); // 100 days
echo $interval->seconds; // outputs 8640000

Alternatively, you can create an interval from a PHP DateInterval, or from an interval specification with the following static methods:, (*5)

use SavvyWombat\Ahora\Interval;

$date1 = new \DateTime("now");
$date2 = new \DateTime("2018-01-01");

$interval = Interval::createFromDateInterval($date1->diff($date2));

// or

$interval = Interval::createFromIntervalSpec("P28DT6H42M12S");

Interval math

Once you have an interval, you can easily add a number of seconds, minutes, hours, or days:, (*6)

$interval = new Interval();

$interval->addSeconds(60); // equivalent to $interval->addMinutes(1);
$interval->addMinutes(60); // equivalent to $interval->addHours(1);
$interval->addHours(24); // equivalent to $interval->addDays(1);
$interval->addDays(7);

Adding intervals

It is also possible to add intervals together:, (*7)

$firstInterval = new Interval();
$firstInterval->addSeconds(45);

$secondInterval = new Interval();
$secondInterval->addSeconds(70);

$firstInterval->addInterval($secondInterval);

echo $firstInterval->seconds; // outputs 55
echo $firstInterval->minutes; // outputs 1

echo $firstInterval->realSeconds; // outputs 115

Subtracting intervals

Similarly, you can subtract an interval from another:, (*8)

$firstInterval = new Interval();
$firstInterval->addSeconds(45);

$secondInterval = new Interval();
$secondInterval->addSeconds(200);

$firstInterval->subInterval($secondInterval);

echo $firstInterval->seconds; // outputs -35
echo $firstInterval->minutes; // outputs -2

echo $firstInterval->realSeconds; // outputs -155

Units and factors

It is possible to inject additional units into the interval (such as weeks)., (*9)

You can even modify or completely replace the units that the interval uses - with the limitation that one unit must be a multiple of seconds, (*10)

$interval = new Interval();
$interval->addDays(10);

echo $interval->days; // outputs 10

$interval->setFactor('weeks', [7, 'days']);

echo $interval->realHours; // outputs 240
echo $interval->days; // outputs 3
echo $interval->weeks; // outputs 1


$interval->setFactor('days', [8, 'hours']);
$interval->setFactor('weeks', [5, 'days']);

echo $interval->realHours; // outputs 240
echo $interval->days; // outputs 2
echo $interval->weeks; // outputs 4

Obviously, changing these multipliers can have some drastic effects on the interval's outputs., (*11)

$interval = new Interval();

$oldFactors = $interval->getFactors(); // [ 
                                       //   'minutes' => [60, 'seconds'], 
                                       //   'hours' => [60, 'minutes'],
                                       //   'days' => [24, 'hours'],
                                       // ]

$interval->setFactors([
    'microts' => [1, 'seconds'], // one unit must be related to seconds
    'arns' => [3600, 'microts'],
    'days' => [24, 'arns'],
    'cycles' => [360, 'days'],
]);

$interval->addDays(400);

echo $interval->cycles; // outputs 1
echo $interval->days; // outputs 40

Support

Please report issues using the GitHub issue tracker. You are also welcome to fork the repository and submit a pull request., (*12)

Licence

Ahora is licensed under The MIT License (MIT)., (*13)

The Versions

28/06 2018

dev-master

9999999-dev https://github.com/SavvyWombat/ahora

Interval calculator to help sum up multiple time intervals and display the total elapsed days/hours/minutes/seconds

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Stuart Jones

date time datetime interval savvywombat savvy wombat ahora

28/06 2018

1.0.1

1.0.1.0 https://github.com/SavvyWombat/ahora

Interval calculator to help sum up multiple time intervals and display the total elapsed days/hours/minutes/seconds

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Stuart Jones

date time datetime interval savvywombat savvy wombat ahora

28/06 2018

1.0

1.0.0.0 https://github.com/SavvyWombat/ahora

DateTime extension

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Stuart Jones

date time datetime interval savvywombat savvy wombat ahora