Time processing library, providing format judgment, time increase and decrease, etc.
Continuation library marshung/helper, only keep and maintain DatetimeHelper
# composer require marsapp/datetimehelper
Include composer autoloader before use.
require __PATH__ . "vendor/autoload.php";
Namespace use:
use marsapp\helper\datetime\DatetimeHelper;
Determine if the date is legal
isDate($date) : Bool
Example :
if (DatetimeHelper::isDate('2019-01-15)) {
die('Date format error');
}
Date calculation - increase
dateAdd(String $date, Int $add = '1', String $unit = 'day', String $format = 'Y-m-d') : string
$unit: day, month, year, hour, minute, second
Example :
DatetimeHelper::dateAdd('2019-01-31', '1', 'day');
// result: 2019-02-01
DatetimeHelper::dateAdd('2019-01-31', '1', 'month');
// result: 2019-02-28
DatetimeHelper::dateAdd('2019-01-31 12:34:56', '13', 'month', 'Y-m-d H:i:s');
// result: 2020-02-29 12:34:56
DatetimeHelper::dateAdd('2019-01-31 12:34:56', '20', 'hour', 'Y-m-d H:00:00');
// result: 2019-02-01 09:00:00
Date calculation - reduction
dateReduce(String $date, Int $reduce = '1', String $unit = 'day', String $format = 'Y-m-d') : string
$unit: day, month, year, hour, minute, second
Example :
DatetimeHelper::dateReduce('2019-01-01', '1', 'day');
// result: 2018-12-31
DatetimeHelper::dateReduce('2019-01-31', '2', 'month');
// result: 2018-11-30
DatetimeHelper::dateReduce('2020-02-29 12:34:56', '1', 'year', 'Y-m-d H:i:s');
// result: 2019-02-28 12:34:56
Date calculation
dateCal(String $date, Int $difference = '1', String $unit = 'day', String $format = 'Y-m-d') : string
$difference positive is add, negative is reduce
$unit: day, month, year, hour, minute, second
Example :
DatetimeHelper::dateCal('2019-01-31', '1', 'month');
// result: 2019-02-28
DatetimeHelper::dateCal('2020-02-29', '-1', 'year');
// result: 2019-02-28
Get Date Iterator - Iteration Date in Days
dateIterator($startDate, $endDate)
Example :
$daterange = DateTimeHelper::dateIterator('2018-01-01', '2018-01-31');
foreach($daterange as $date){
echo $date->format('Y-m-d') . '<br>';
}