magroski / time-buddy
A library to handle time and date interval
Installs: 17 705
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.1 || ^8
Requires (Dev)
- object-calisthenics/phpcs-calisthenics-rules: ^3.1
- phpstan/phpstan: ^0.10.0
- phpunit/phpunit: ^7.5
- roave/security-advisories: dev-master
- slevomat/coding-standard: 4.5
- squizlabs/php_codesniffer: *
This package is auto-updated.
Last update: 2024-10-28 23:26:17 UTC
README
This library provides some syntax sugar around time and date manipulation, including on-the-fly format localization without the need to add multiple locales to your environment.
Locales are available on the folder src/Provider
. Pull-requests are welcome.
Usage examples
Creation
# Default $time = new Time(); # From unix timestamp $time = Time::createFromUnixTstamp(time()); # From DateTime $dateTime = new \DateTime(); $time = Time::createFromDateTime($dateTime); # From DateTimeImmutable $immutable = new \DateTimeImmutable(); $time = new Time($immutable);
Operations (Time is immutable, so operations generate new objects)
$firstTime = new Time(); $secondTime = $firstTime->add(100); $thirdTime = $firstTime->subtract(100);
Formatting
$time = new Time(); $time->format('d F Y'); # 20 May 2019 $time->setLocale('pt_BR'); $time->format('d F Y'); # 20 Maio 2019
Generating a Date Interval
$time = new Time(); $laterTime = new Time(); # From Time $interval = $time->diff($laterTime); # Staticaly $interval = DateInterval::createFromTime($time, $laterTime); # Staticaly from a single time $interval = DateInterval::createFromTime($time); # Second argument is current time