PHP Library to simplify stuff when all you want is a number of seconds.
Will be kept super simple as it's mainly just a single Enum class with a bunch of constants and helper functions that all return
an integer. Don't expect too many new features in upcoming patches and updates, since it's just a simple helper library that does
one thing and one thing only.
Usage is super simple, as one would expect. Just call the static method for the appropriate input type and give it the number,
and it will return the number of seconds. No more 60 * 60 * 24 * 7 to get the number of seconds in a week. Just call
Seconds::fromWeeks(1) and you're good to go.
For example:
Seconds::fromMinutes(1);
// 60
Seconds::fromMinutes(32);
// 1920
Seconds::fromHours(3);
// 180
Seconds::fromDays(3)
// 259200
Secnds::fromWeeks(4)
// 2419200Since I want to keep things simple, the output is limited to positive numbers. This means that even if the input is negative, the output will be positive:
Seconds::fromMinutes(-1);
// 60Also, the input for all methods must be a whole number, so you cannot check how many seconds are in 3.5 hours unless you do something like this:
Seconds::fromHours(3) + Seconds::fromMinutes(30);
// 12600Finally, the longest available time period is weeks, since both months and years have a variable length. If you want to figure out how many seconds are in three months, you'll have to do something like one of these examples:
use Breki\Seconds;Seconds::fromWeeks(13);
// 7862400
Seconds::fromDays(31 + 30 + 31);
// 7948800Right now, I have very little planned for this library as it does what it's supposed to do. The only thing that I can imagine
is maybe adding support for calculating the seconds between two dates, but that's not really the purpose of this library. If
you want to do that, you should probably use the Carbon library and do something like this:
$date1 = Carbon::parse('2023-09-23 07:14:00 UTC')
$date2 = Carbon::parse('2023-12-31 23:59:59 UTC')
$date1->diffInSeconds($date2)
> 8613959Of course, if you have any thoughts or ideas, feel free to open an issue or throw a Pull Request my way, and we'll see what comes of it!