Chronos , PHP 时间日历处理库,零依赖。
使用 composer 安装
php composer.phar require "cakephp/chronos:^2.0"
- Cake\Chronos\Chronos 是不可变的日期和时间对象。
- Cake\Chronos\Date 是不可变的日期对象。 is a immutable date object.
- Cake\Chronos\MutableDateTime 是可变的日期和时间对象。 is a mutable date and time object.
- Cake\Chronos\MutableDate 是可变的日期对象。 is a mutable date object.
- Cake\Chronos\ChronosInterval 是 DateInterval 的扩展。 is an extension to the DateInterval object.
引入头部
use Cake\Chronos\Chronos;
实例创建
<?php
use Cake\Chronos\Chronos;
$now = Chronos::now();
$today = Chronos::today();
$yesterday = Chronos::yesterday();
$tomorrow = Chronos::tomorrow();
//
$time = new Chronos('2015-12-31 23:59:58.123');
// Parse relative expressions
$date = Chronos::parse('+2 days, +3 hours');
// Date and time integer values.
$date = Chronos::create(2015, 12, 25, 4, 32, 58);
// Date or time integer values.
$date = Chronos::createFromDate(2015, 12, 25);
$date = Chronos::createFromTime(11, 45, 10);
// Parse formatted values.
$date = Chronos::createFromFormat('m/d/Y', '06/15/2015');
可变 和 不可变对象
<?php
// 可变对象可以处理,不可变对象改处理方式不起作用
$time->addDay(1);
doSomething($time);
return $time;
// 不可变对象工作方式
$time = $time->addDay(1);
$time = doSomething($time);
return $time;
日期对象
<?php
use Cake\Chronos\Date;
$today = Date::today();
// Changes to the time/timezone are ignored.
$today->modify('+1 hours');
// Outputs '2015-12-20'
echo $today;
<?php
use Cake\Chronos\Date:
// Takes the current date from Asia/Tokyo time zone
$today = Date::today('Asia/Tokyo');
修改方法
<?php
// 设定日期
$halloween = Chronos::create()
->year(2015)
->month(10)
->day(31)
->hour(20)
->minute(30);
// 修改时间
$future = Chronos::create()
->addYear(1)
->subMonth(2)
->addDays(15)
->addHours(20)
->subMinutes(2);
// 获取特定的时间
$time = Chronos::create();
$time->startOfDay();
$time->endOfDay();
$time->startOfMonth();
$time->endOfMonth();
$time->startOfYear();
$time->endOfYear();
$time->startOfWeek();
$time->endOfWeek();
// 周时间
$time->next(ChronosInterface::TUESDAY);
$time->previous(ChronosInterface::MONDAY);
// DST 转化为 UTC 时间后处理
// Additional hour gained.
$time = new Chronos('2014-03-30 00:00:00', 'Europe/London');
debug($time->modify('+24 hours')); // 2014-03-31 01:00:00
// First switch to UTC, and modify
$time = $time->setTimezone('UTC')->modify('+24 hours');
比较
char | u | title |
---|---|---|
gt | > | 大于 |
lt | < | 小于 |
gte | >= | 大于等于 |
lte | <= | 小于等于 |
eq | = | 等于 |
ne | != | 不等于 |
<?php
// Full suite of comparators exist
// ne, gt, lt, lte.
$first->eq($second);
$first->gte($second);
$first->gt($second); // >
$first->lte($second);
$first->lt($second); // <
$first->ne($second);
// See if the current object is between two others.
$now->between($start, $end);
// Find which argument is closest or farthest.
$now->closest($june, $november);
$now->farthest($june, $november);
// 日历处理
$now->isToday();
$now->isYesterday();
$now->isFuture();
$now->isPast();
// Check the day of the week
$now->isWeekend();
// All other weekday methods exist too.
$now->isMonday();
// 是否在一个时间段内
$time->wasWithinLast('3 days');
$time->isWithinNext('3 hours');
差异处理
<?php
// Get a DateInterval representing the difference
$first->diff($second);
// Get difference as a count of specific units.
$first->diffInHours($second);
$first->diffInDays($second);
$first->diffInWeeks($second);
$first->diffInYears($second);
// Difference from now.
echo $date->diffForHumans();
// Difference from another point in time.
echo $date->diffForHumans($other); // 1 hour ago;
格式化处理
<?php
// Uses the format controlled by setToStringFormat()
echo $date;
echo $time->format('Y-m-d H:i:s'); // toFormattedDateString
// Different standard formats
echo $time->toAtomString(); // 1975-12-25T14:15:16-05:00
echo $time->toCookieString(); // Thursday, 25-Dec-1975 14:15:16 EST
echo $time->toIso8601String(); // 1975-12-25T14:15:16-05:00
echo $time->toRfc822String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc850String(); // Thursday, 25-Dec-75 14:15:16 EST
echo $time->toRfc1036String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc1123String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc2822String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc3339String(); // 1975-12-25T14:15:16-05:00
echo $time->toRssString(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toW3cString(); // 1975-12-25T14:15:16-05:00
// Get the quarter/week
echo $time->toQuarter(); // 4
echo $time->toWeek(); // 52
// Generic formatting
echo $time->toTimeString(); // 14:15:16
echo $time->toDateString(); // 1975-12-25
echo $time->toDateTimeString(); // 1975-12-25 14:15:16
echo $time->toFormattedDateString(); // Dec 25, 1975
echo $time->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM
日期时间
<?php
$time = new Chronos('2015-12-31 23:59:58.123');
$time->year; // 2015
$time->month; // 12
$time->day; // 31
$time->hour // 23
$time->minute // 59
$time->second // 58
$time->micro // 123
$time->timezone
$time->timezoneName
$time->dayOfWeek
$time->dayOfMonth
$time->dayOfYear
$time->daysInMonth
$time->timestamp
$time->quarter
参考: