Skip to content

DatetimeHelper library, providing format judgment, time increase and decrease, etc.

License

Notifications You must be signed in to change notification settings

marshung24/DatetimeHelper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DatetimeHelper

Time processing library, providing format judgment, time increase and decrease, etc.

Continuation library marshung/helper, only keep and maintain DatetimeHelper

Latest Stable Version Total Downloads Latest Unstable Version License

Outline

Composer Install

# composer require marsapp/datetimehelper

Include

Include composer autoloader before use.

require __PATH__ . "vendor/autoload.php";

Namespace use:

use marsapp\helper\datetime\DatetimeHelper;

isDate()

Determine if the date is legal

isDate($date) : Bool

Example :

if (DatetimeHelper::isDate('2019-01-15)) {
	die('Date format error');
}

dateAdd()

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

dateReduce()

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

dateCal()

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

dateIterator()

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>';
}

About

DatetimeHelper library, providing format judgment, time increase and decrease, etc.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages