y0lk / php-mysql-interval
Convert DateInterval to MySQL temporal INTERVAL expressions
Installs: 2 735
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.1.0
Requires (Dev)
- phpunit/phpunit: ^7.4
This package is auto-updated.
Last update: 2024-11-21 20:59:09 UTC
README
Tiny PHP class to convert DateInterval objects to MySQL temporal interval expressions
Installation
Via Composer
$ composer require y0lk/php-mysql-interval
Usage
use Y0lk\MySQLInterval\MySQLInterval; //---Example with a DateInterval create from an interval specification echo MySQLInterval::fromDateInterval(new DateInterval("P1D"), "DAY"); //Outputs "INTERVAL 1 DAY" //---Example using diff between 2 dates $date1 = new DateTime("2000-01-01 05:20:11.000000"); $date2 = new DateTime("2000-01-21 12:55:33.123456"); $interval = $date1->diff($date2); $expr = MySQLInterval::fromDateInterval($interval, "DAY_MICROSECOND"); //Ouputs "INTERVAL '20 7:35:22.123456' DAY_MICROSECOND" //---Example using a date string with fromDateString() echo MySQLInterval::fromDateString("300 days + 7 hours", "DAY_HOUR"); //Outputs "INTERVAL '300 7' DAY_HOUR"
License
The MIT License (MIT). Please see License File for more information.