y0lk/php-mysql-interval

Convert DateInterval to MySQL temporal INTERVAL expressions

0.1 2018-11-21 03:01 UTC

This package is auto-updated.

Last update: 2024-11-21 20:59:09 UTC


README

Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality License Total Downloads

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.