类日期
扩展 Chronos 提供的 Date 类。
添加了方便的方法和区域感知格式化辅助函数。
常量
-
字符串DEFAULT_TO_STRING_FORMAT ¶'Y-m-d'类型转换发生时,用于 __toString 方法的默认格式。
属性摘要
-
$_jsonEncodeFormat protected static
Closure|string|int将此对象转换为 JSON 时使用的格式。
-
$_toStringFormat protected static
string|int使用
Cake\I18n\Date::i18nFormat()和__toString格式化时间时使用的格式。 -
$age public @property-read
int使用默认参数执行 diffInYears()
-
$day public @property-read
int<1, 31> -
$dayOfWeek public @property-read
int<1, 7>1(星期一)到 7(星期日)
-
$dayOfYear public @property-read
int<0, 365>0 到 365
-
$days protected static
array一周中每一天的名称。
-
$daysInMonth public @property-read
int<1, 31>给定月份的天数
-
$diffFormatter protected static
Cake\Chronos\DifferenceFormatterInterface|null差异格式化对象的实例。
-
$formatters protected static
array<string,IntlDateFormatter>日期格式化程序的内存缓存
-
$half public @property-read
int<1, 2>一年中的前半部分,1 代表 1 月到 6 月,2 代表 7 月到 12 月。
-
$lastErrors protected static
array|false上次调用 createFromFormat() 时的错误。
-
$month public @property-read
int<1, 12> -
$native protected
DateTimeImmutable -
$niceFormat public static
string|int使用
Cake\I18n\Date::nice()格式化时间时使用的格式 -
$quarter public @property-read
int<1, 4>此实例所在的季度,1 到 4
-
$toStringFormat protected static
字符串类型转换发生时,用于 __toString 方法的格式。
-
$weekOfMonth public @property-read
int<1, 5>1 到 5
-
$weekOfYear public @property-read
int<1, 53>ISO-8601 年份的周数,以星期一开始
-
$wordAccuracy public static
array<string, string>使用
Date::timeAgoInWords()格式化时间且差异小于Date::$wordEnd时使用的格式 -
$wordEnd public static
字符串相对时间提示的结束时间
-
$wordFormat public static
string|int使用
Cake\I18n\Date::timeAgoInWords()格式化时间且差异大于Cake\I18n\Date::$wordEnd时使用的格式 -
$year public @property-read
int -
$yearIso public @property-read
int
方法摘要
-
__construct() public
创建一个新的 Immutable Date 实例。
-
__debugInfo() public
返回用于调试的属性。
-
__get() public
获取对象的一部分
-
__isset() public
检查对象上是否存在属性
-
__toString() public
返回由
setToStringFormat()或默认的DEFAULT_TO_STRING_FORMAT格式指定的格式化字符串。 -
_formatObject() protected
返回翻译并本地化的日期字符串。实现 PHP 5.5+ 中的 IntlDateFormatter::formatObject()。
-
_parseDateTime() protected static
根据传递的或配置的日期时间格式,解析提供的时
-
add() public
将时间间隔添加到日期
-
addDays() public
将天数添加到实例。正 $value 前移,负 $value 后移。
-
addMonths() public
将月数添加到实例。正 $value 前移,负 $value 后移。
-
addMonthsWithOverflow() public
将月数(含溢出)添加到实例。正 $value 前移,负 $value 后移。
-
addWeekdays() public
将工作日添加到实例。正 $value 前移,负 $value 后移。
-
addWeeks() public
将周数添加到实例。正 $value 前移,负 $value 后移。
-
addYears() public
将年数添加到实例。正 $value 前移,负 $value 后移。
-
addYearsWithOverflow() public
将年数(含溢出)添加到实例。正 $value 前移,负 $value 后移。
-
between() public
确定实例是否位于其他两个实例之间
-
closest() public
获取距离实例最近的日期。
-
create() public static
从特定日期创建一个实例。
-
createFromArray() public static
从日期值的数组创建实例。
-
createFromFormat() public static
从特定格式创建实例
-
createNative() protected
初始化 PHP DateTimeImmutable 对象。
-
day() public
设置实例的天数
-
diff() public
返回此实例和目标之间的差异。
-
diffFiltered() public
使用过滤器可调用函数获取给定时间间隔的差异
-
diffForHumans() public
以人类可读的格式获取差异。
-
diffFormatter() public static
获取差异格式化程序实例。
-
diffInDays() public
获取天数的差异
-
diffInDaysFiltered() public
使用过滤器可调用函数获取天数的差异
-
diffInMonths() public
获取月数的差异
-
diffInWeekdays() public
获取工作日的差异
-
diffInWeekendDays() public
使用过滤器获取周末天数的差异
-
diffInWeeks() public
获取周数的差异
-
diffInYears() public
获取年数的差异
-
endOfCentury() public
将日期重置为世纪末,时间重置为 23:59:59
-
endOfDecade() public
将日期重置为十年末
-
endOfMonth() public
将日期重置为月末
-
endOfWeek() public
将日期重置为周末(在 $weekEndsAt 中定义)并且时间为 23:59:59
-
endOfYear() public
将日期重置为年末
-
equals() public
确定实例是否等于另一个实例
-
farthest() public
获取距实例最远的日期。
-
firstOfMonth() public
修改为当前月份中给定一周的第一个出现。如果没有提供 dayOfWeek,则修改为当前月份的第一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
-
firstOfQuarter() public
修改为当前季度中给定一周的第一个出现。如果没有提供 dayOfWeek,则修改为当前季度的第一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
-
firstOfYear() public
修改为当前年份中给定一周的第一个出现。如果没有提供 dayOfWeek,则修改为当前年份的第一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
-
format() public
根据 DateTimeImmutable::format() 返回格式化的日期字符串。
-
getLastErrors() public static
返回上次
createFromFormat()调用的解析警告和错误。 -
greaterThan() public
确定实例是否大于(晚于)另一个实例
-
greaterThanOrEquals() public
确定实例是否大于(晚于)或等于另一个实例
-
i18nFormat() public
使用指定区域设置的优选格式和语言返回此时间对象的格式化字符串。
-
isFirstHalf() public
确定实例是否在年上半年内
-
isFriday() public
检查这一天是否是星期五。
-
isFuture() public
确定实例是否在未来,即大于(晚于)现在
-
isLastMonth() public
确定实例是否在过去一个月内
-
isLastWeek() public
确定实例是否在过去一周内
-
isLastYear() public
确定实例是否在过去一年内
-
isLeapYear() public
确定实例是否为闰年
-
isMonday() public
检查这一天是否是星期一。
-
isNextMonth() public
确定实例是否在下个月内
-
isNextWeek() public
确定实例是否在下周内
-
isNextYear() public
确定实例是否在来年内
-
isPast() public
确定实例是否在过去,即小于(早于)现在
-
isSaturday() public
检查这一天是否是星期六。
-
isSecondHalf() public
确定实例是否在年下半年内
-
isSunday() public
检查这一天是否是星期日。
-
isThursday() public
检查这一天是否是星期四。
-
isToday() public
确定实例是否为今天
-
isTomorrow() public
确定实例是否为明天
-
isTuesday() public
检查这一天是否是星期二。
-
isWednesday() public
检查这一天是否是星期三。
-
isWeekday() public
确定实例是否为工作日
-
isWeekend() public
确定实例是否为周末
-
isWithinNext() public
如果此实例将在指定的时间间隔内发生,则返回 true
-
isYesterday() public
确定实例是否为昨天
-
jsonSerialize() public
返回将此对象转换为 JSON 时应序列化的字符串
-
lastOfMonth() public
修改为当前月份中给定一周的最后一次出现。如果没有提供 dayOfWeek,则修改为当前月份的最后一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
-
lastOfQuarter() public
修改为当前季度中给定一周的最后一次出现。如果没有提供 dayOfWeek,则修改为当前季度的最后一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
-
lastOfYear() public
修改为当前年份中给定一周的最后一次出现。如果没有提供 dayOfWeek,则修改为当前年份的最后一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
-
lessThan() public
确定实例是否小于(早于)另一个实例
-
lessThanOrEquals() public
确定实例是否小于(早于)或等于另一个实例
-
modify() public
创建一个新的实例,其日期根据 DateTimeImmutable::modifier() 修改。
-
month() public
设置实例的月份
-
next() public
修改为给定一周的下一个出现。如果没有提供 dayOfWeek,则修改为当前一周的下一个出现。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
-
nice() public
返回此对象的格式良好的日期字符串。
-
notEquals() public
确定实例是否不等于另一个实例
-
now() public static
获取今天的日期。
-
nthOfMonth() public
修改为当前月份中给定一周的给定出现。如果计算出的出现超出当前月份的范围,则返回 false,并且不会进行任何修改。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
-
nthOfQuarter() public
修改为当前季度中给定一周的给定出现。如果计算出的出现超出当前季度的范围,则返回 false,并且不会进行任何修改。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
-
nthOfYear() public
修改为当前年份中给定一周的给定出现。如果计算出的出现超出当前年份的范围,则返回 false,并且不会进行任何修改。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
-
parse() public static
从字符串创建实例。这是构造函数的别名,它允许更好的流畅语法,因为它允许你执行 Chronos::parse('Monday next week')->fn() 而不是 (new Chronos('Monday next week'))->fn()
-
parseDate() public static
根据传递的或配置的格式解析提供的 $date 字符串后,返回一个新的 Date 对象。此方法依赖于区域设置,传递给此函数的任何字符串都将被解释为依赖于区域设置的字符串。
-
previous() public
修改为给定一周的先前出现。如果没有提供 dayOfWeek,则修改为当前一周的先前出现。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
-
resetToStringFormat() public static
将 __toString() 格式重置为
DEFAULT_TO_STRING_FORMAT。 -
setDate() public
设置日期。
-
setISODate() public
根据 ISO 8601 标准设置日期
-
setJsonEncodeFormat() public static
设置将此对象转换为 JSON 时使用的默认格式
-
setToStringFormat() public static
设置将此类型实例类型转换为字符串时使用的默认格式
-
startOfCentury() public
将日期重置为世纪的第一天
-
startOfDecade() public
将日期重置为十年的第一天
-
startOfMonth() public
将日期重置为月初
-
startOfWeek() public
将日期重置为一周的第一天(在 $weekStartsAt 中定义)
-
startOfYear() public
将日期重置为年初
-
sub() public
从日期中减去一个时间间隔。
-
subDays() public
从实例中删除天数
-
subMonths() public
从实例中删除月份
-
subMonthsWithOverflow() public
将月数(含溢出)添加到实例。正 $value 前移,负 $value 后移。
-
subWeekdays() public
从实例中删除工作日
-
subWeeks() public
从实例中删除周数
-
subYears() public
从实例中删除年数。
-
subYearsWithOverflow() public
从实例中删除带溢出的年数
-
timeAgoInWords() public
根据当前日期和此对象之间的差异返回相对日期或格式化的绝对日期。
-
toAtomString() public
将实例格式化为 ATOM
-
toCookieString() public
将实例格式化为 COOKIE
-
toDateString() public
将实例格式化为日期
-
toDateTimeImmutable() public
以午夜的
DateTimeImmutable实例的形式返回日期。 -
toDateTimeString() public
将实例格式化为日期和时间
-
toDayDateTimeString() public
使用日期、日期和时间格式化实例。
-
toFormattedDateString() public
将实例格式化为可读的日期。
-
toIso8601String() public
将实例格式化为 ISO8601。
-
toNative() public
以午夜的
DateTimeImmutable实例的形式返回日期。 -
toQuarter() public
返回季度。
-
toRfc1036String() public
将实例格式化为 RFC1036。
-
toRfc1123String() public
将实例格式化为 RFC1123。
-
toRfc2822String() public
将实例格式化为 RFC2822。
-
toRfc3339String() public
将实例格式化为 RFC3339。
-
toRfc822String() public
将实例格式化为 RFC822。
-
toRfc850String() public
将实例格式化为 RFC850。
-
toRssString() public
将实例格式化为 RSS。
-
toTimeString() public
将实例格式化为时间。
-
toUnixString() public
返回 UNIX 时间戳。
-
toW3cString() public
将实例格式化为 W3C。
-
toWeek() public
返回 ISO 8601 年份的周数,周从星期一开始。
-
today() public static
获取今天的日期。
-
tomorrow() public static
获取明天的日期。
-
wasWithinLast() public
如果此实例发生在指定的时间间隔内,则返回 true。
-
year() public
设置实例的年份。
-
yesterday() public static
获取昨天的日期。
方法详情
__construct() ¶ public
__construct(Cake\Chronos\ChronosDateDateTimeInterface|string $time = 'now', DateTimeZone|string|null $timezone = null)
创建一个新的 Immutable Date 实例。
日期没有时间或时区组件。在内部,ChronosDate 包装了一个 PHP DateTimeImmutable,但限制修改只能对那些对日期值起作用的修改。
默认情况下,日期将根据服务器的默认时区计算。您可以使用 timezone 参数使用不同的时区。时区在解析像 today 和 yesterday 这样的相对日期表达式时使用,但在解析像 2022-01-01 这样的值时不参与。
参数
-
Cake\Chronos\ChronosDateDateTimeInterface|string$time optional 固定或相对时间
-
DateTimeZone|string|null$timezone optional 用于“现在”的时区
__get() ¶ public
__get(string $name): string|float|int|bool
获取对象的一部分
参数
-
string$name 要读取的属性名称。
返回
string|float|int|bool抛出
InvalidArgumentException__toString() ¶ public
__toString(): string
返回由 setToStringFormat() 或默认的 DEFAULT_TO_STRING_FORMAT 格式指定的格式化字符串。
返回
字符串_formatObject() ¶ protected
_formatObject(DateTimeInterface $date, array<int>|string $format, string|null $locale): string
返回翻译并本地化的日期字符串。实现 PHP 5.5+ 中的 IntlDateFormatter::formatObject()。
参数
-
DateTimeInterface$date 日期。
-
array<int>|string$format 格式。
-
string|null$locale 应该显示日期的区域设置名称。
返回
字符串_parseDateTime() ¶ protected static
_parseDateTime(string $time, array<int>|string $format, DateTimeZone|string|null $tz = null): static|null
根据传递的或配置的日期时间格式,解析提供的时
与 DateTime 不同,返回的实例的时区始终转换为 $tz(如果为空则为默认时区),即使 $time 字符串指定了时区。这是 IntlDateFormatter 的限制。
如果无法解析提供的日期,则将返回 null。
示例
$time = Time::parseDateTime('10/13/2013 12:54am');
$time = Time::parseDateTime('13 Oct, 2013 13:54', 'dd MMM, y H:mm');
$time = Time::parseDateTime('10/10/2015', [IntlDateFormatter::SHORT, IntlDateFormatter::NONE]);
参数
-
string$time 要解析的时间字符串。
-
array<int>|string$format IntlDateFormatter 接受的任何格式。
-
DateTimeZone|string|null$tz optional 实例的时区
返回
static|nulladd() ¶ public
add(DateInterval $interval): static
将时间间隔添加到日期
任何对时间更改都将被忽略并重置为 00:00:00
参数
-
DateInterval$interval 要修改此日期的时间间隔。
返回
staticaddDays() ¶ public
addDays(int $value): static
将天数添加到实例。正 $value 前移,负 $value 后移。
参数
-
int$value 要添加的天数。
返回
staticaddMonths() ¶ public
addMonths(int $value): static
将月数添加到实例。正 $value 前移,负 $value 后移。
在添加或减去月份时,如果结果时间是日期不存在的日期,则此操作的结果将始终是目标月份的最后一天。
示例
(new Chronos('2015-01-03'))->addMonths(1); // Results in 2015-02-03
(new Chronos('2015-01-31'))->addMonths(1); // Results in 2015-02-28
参数
-
int$value 要添加的月份数。
返回
staticaddMonthsWithOverflow() ¶ public
addMonthsWithOverflow(int $value): static
将月数(含溢出)添加到实例。正 $value 前移,负 $value 后移。
如果新的 ChronosDate 不存在,则日期溢出到下个月。
示例
(new Chronos('2012-01-30'))->addMonthsWithOverflow(1); // Results in 2013-03-01
参数
-
int$value 要添加的月份数。
返回
staticaddWeekdays() ¶ public
addWeekdays(int $value): static
将工作日添加到实例。正 $value 前移,负 $value 后移。
参数
-
int$value 要添加的工作日数。
返回
staticaddWeeks() ¶ public
addWeeks(int $value): static
将周数添加到实例。正 $value 前移,负 $value 后移。
参数
-
int$value 要添加的周数。
返回
staticaddYears() ¶ public
addYears(int $value): static
将年数添加到实例。正 $value 前移,负 $value 后移。
如果新的 ChronosDate 不存在,则使用月份的最后一天,而不是溢出到下个月。
示例
(new Chronos('2015-01-03'))->addYears(1); // Results in 2016-01-03
(new Chronos('2012-02-29'))->addYears(1); // Results in 2013-02-28
参数
-
int$value 要添加的年份数。
返回
staticaddYearsWithOverflow() ¶ public
addYearsWithOverflow(int $value): static
将年数(含溢出)添加到实例。正 $value 前移,负 $value 后移。
如果新的 ChronosDate 不存在,则日期溢出到下个月。
示例
(new Chronos('2012-02-29'))->addYearsWithOverflow(1); // Results in 2013-03-01
参数
-
int$value 要添加的年份数。
返回
staticbetween() ¶ public
between(Cake\Chronos\ChronosDate $start, Cake\Chronos\ChronosDate $end, bool $equals = true): bool
确定实例是否位于其他两个实例之间
参数
-
Cake\Chronos\ChronosDate$start 目标范围的开始
-
Cake\Chronos\ChronosDate$end 目标范围的结束
-
bool$equals optional 是否包括范围的开始和结束
返回
boolclosest() ¶ public
closest(Cake\Chronos\ChronosDate $first, Cake\Chronos\ChronosDate $second, Cake\Chronos\ChronosDate ...$others): self
获取距离实例最近的日期。
参数
-
Cake\Chronos\ChronosDate$first 要比较的实例。
-
Cake\Chronos\ChronosDate$second 要比较的实例。
-
Cake\Chronos\ChronosDate...$others 要比较的其他实例。
返回
selfcreate() ¶ public static
create(int $year, int $month, int $day): static
从特定日期创建一个实例。
参数
-
int$year 要创建实例的年份。
-
int$month 要创建实例的月份。
-
int$day 要创建实例的日期。
返回
staticcreateFromArray() ¶ public static
createFromArray(array<int|string> $values): static
从日期值的数组创建实例。
允许的值
- year
- month
- day
参数
-
array<int|string>$values 日期和时间值的数组。
返回
staticcreateFromFormat() ¶ public static
createFromFormat(string $format, string $time): static
从特定格式创建实例
参数
-
string$format 与 date() 兼容的格式字符串。
-
string$time 要解释的格式化日期字符串。
返回
static抛出
InvalidArgumentExceptioncreateNative() ¶ protected
createNative(Cake\Chronos\ChronosDateDateTimeInterface|string $time, DateTimeZone|string|null $timezone): DateTimeImmutable
初始化 PHP DateTimeImmutable 对象。
参数
-
Cake\Chronos\ChronosDateDateTimeInterface|string$time 固定或相对时间
-
DateTimeZone|string|null$timezone 用于“现在”的时区
返回
DateTimeImmutablediff() ¶ public
diff(Cake\Chronos\ChronosDate $target, bool $absolute = false): DateInterval
返回此实例和目标之间的差异。
参数
-
Cake\Chronos\ChronosDate$target 目标实例
-
bool$absolute optional 时间间隔是否强制为正
返回
DateIntervaldiffFiltered() ¶ public
diffFiltered(DateInterval $interval, callable $callback, Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true, int $options = 0): int
使用过滤器可调用函数获取给定时间间隔的差异
参数
-
DateInterval$interval 要遍历的时间间隔
-
callable$callback 用于过滤的回调函数。
-
Cake\Chronos\ChronosDate|null$other optional 要从其差异化的实例。
-
bool$absolute optional 获取差异的绝对值
-
int$options optional DatePeriod 选项,{@see https://php.ac.cn/manual/en/class.dateperiod.php}
返回
intdiffForHumans() ¶ public
diffForHumans(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = false): string
以人类可读的格式获取差异。
将过去的值与默认现在进行比较时:5 个月前
将未来值与默认现在进行比较时:5 个月后
将过去值与另一个值进行比较时:5 个月前
将未来值与另一个值进行比较时:5 个月后
参数
-
Cake\Chronos\ChronosDate|null$other optional 要比较的日期时间。
-
bool$absolute optional 移除差异修饰符 ago、after 等
返回
字符串diffFormatter() ¶ public static
diffFormatter(Cake\Chronos\DifferenceFormatterInterface|null $formatter = null): Cake\I18n\RelativeTimeFormatter
获取差异格式化程序实例。
参数
-
Cake\Chronos\DifferenceFormatterInterface|null$formatter optional 差异格式化程序
返回
Cake\I18n\RelativeTimeFormatterdiffInDays() ¶ public
diffInDays(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true): int
获取天数的差异
参数
-
Cake\Chronos\ChronosDate|null$other optional 要从其差异化的实例。
-
bool$absolute optional 获取差异的绝对值
返回
intdiffInDaysFiltered() ¶ public
diffInDaysFiltered(callable $callback, Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true, int $options = 0): int
使用过滤器可调用函数获取天数的差异
参数
-
callable$callback 用于过滤的回调函数。
-
Cake\Chronos\ChronosDate|null$other optional 要从其差异化的实例。
-
bool$absolute optional 获取差异的绝对值
-
int$options optional DatePeriod 选项,{@see https://php.ac.cn/manual/en/class.dateperiod.php}
返回
intdiffInMonths() ¶ public
diffInMonths(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true): int
获取月数的差异
参数
-
Cake\Chronos\ChronosDate|null$other optional 要从其差异化的实例。
-
bool$absolute optional 获取差异的绝对值
返回
intdiffInWeekdays() ¶ public
diffInWeekdays(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true, int $options = 0): int
获取工作日的差异
参数
-
Cake\Chronos\ChronosDate|null$other optional 要从其差异化的实例。
-
bool$absolute optional 获取差异的绝对值
-
int$options optional DatePeriod 选项,{@see https://php.ac.cn/manual/en/class.dateperiod.php}
返回
intdiffInWeekendDays() ¶ public
diffInWeekendDays(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true, int $options = 0): int
使用过滤器获取周末天数的差异
参数
-
Cake\Chronos\ChronosDate|null$other optional 要从其差异化的实例。
-
bool$absolute optional 获取差异的绝对值
-
int$options optional DatePeriod 选项,{@see https://php.ac.cn/manual/en/class.dateperiod.php}
返回
intdiffInWeeks() ¶ public
diffInWeeks(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true): int
获取周数的差异
参数
-
Cake\Chronos\ChronosDate|null$other optional 要从其差异化的实例。
-
bool$absolute optional 获取差异的绝对值
返回
intdiffInYears() ¶ public
diffInYears(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true): int
获取年数的差异
参数
-
Cake\Chronos\ChronosDate|null$other optional 要从其差异化的实例。
-
bool$absolute optional 获取差异的绝对值
返回
intequals() ¶ public
equals(Cake\Chronos\ChronosDate $other): bool
确定实例是否等于另一个实例
参数
-
Cake\Chronos\ChronosDate$other 要比较的实例。
返回
boolfarthest() ¶ public
farthest(Cake\Chronos\ChronosDate $first, Cake\Chronos\ChronosDate $second, Cake\Chronos\ChronosDate ...$others): self
获取距实例最远的日期。
参数
-
Cake\Chronos\ChronosDate$first 要比较的实例。
-
Cake\Chronos\ChronosDate$second 要比较的实例。
-
Cake\Chronos\ChronosDate...$others 要比较的其他实例。
返回
selffirstOfMonth() ¶ public
firstOfMonth(int|null $dayOfWeek = null): static
修改为当前月份中给定一周的第一个出现。如果没有提供 dayOfWeek,则修改为当前月份的第一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
参数
-
int|null$dayOfWeek optional 要移动到的星期几。
返回
staticfirstOfQuarter() ¶ public
firstOfQuarter(int|null $dayOfWeek = null): static
修改为当前季度中给定一周的第一个出现。如果没有提供 dayOfWeek,则修改为当前季度的第一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
参数
-
int|null$dayOfWeek optional 要移动到的星期几。
返回
staticfirstOfYear() ¶ public
firstOfYear(int|null $dayOfWeek = null): static
修改为当前年份中给定一周的第一个出现。如果没有提供 dayOfWeek,则修改为当前年份的第一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
参数
-
int|null$dayOfWeek optional 要移动到的星期几。
返回
staticformat() ¶ public
format(string $format): string
根据 DateTimeImmutable::format() 返回格式化的日期字符串。
参数
-
string$format String format
返回
字符串getLastErrors() ¶ public static
getLastErrors(): array|false
返回上次 createFromFormat() 调用的解析警告和错误。
返回与 DateTimeImmutable::getLastErrors() 相同的数据。
返回
array|falsegreaterThan() ¶ public
greaterThan(Cake\Chronos\ChronosDate $other): bool
确定实例是否大于(晚于)另一个实例
参数
-
Cake\Chronos\ChronosDate$other 要比较的实例。
返回
boolgreaterThanOrEquals() ¶ public
greaterThanOrEquals(Cake\Chronos\ChronosDate $other): bool
确定实例是否大于(晚于)或等于另一个实例
参数
-
Cake\Chronos\ChronosDate$other 要比较的实例。
返回
booli18nFormat() ¶ public
i18nFormat(string|int|null $format = null, string|null $locale = null): string|int
使用指定区域设置的优选格式和语言返回此时间对象的格式化字符串。
可以指定要显示的字符串的所需格式。您可以将 IntlDateFormatter 常量作为此函数的第一个参数传递,或者传递以下资源中指定的完整 ICU 日期格式字符串: https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax.
示例
$date = new Date('2014-04-20');
$date->i18nFormat(); // outputs '4/20/14' for the en-US locale
$date->i18nFormat(\IntlDateFormatter::FULL); // Use the full date format
$date->i18nFormat('yyyy-MM-dd'); // outputs '2014-04-20'
您可以通过 Date::setToStringFormat() 控制使用的默认格式。
您可以在 https://secure.php.net/manual/en/class.intldateformatter.php 了解可用的 IntlDateFormatter 常量。
如果您需要使用不同的区域设置来显示此时间对象,请将区域设置字符串作为此函数的第三个参数传递。
示例
$date = new Date('2014-04-20');
$time->i18nFormat(null, 'de-DE');
$time->i18nFormat(\IntlDateFormatter::FULL, 'de-DE');
您可以通过 Date::setDefaultLocale() 控制使用的默认区域设置。如果为空,则将从 intl.default_locale ini 配置中获取默认值。
参数
-
string|int|null$format optional 格式字符串。
-
string|null$locale optional 应以其显示日期的区域设置名称(例如 pt-BR)。
返回
string|intisFuture() ¶ public
isFuture(DateTimeZone|string|null $timezone = null): bool
确定实例是否在未来,即大于(晚于)现在
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
boolisLastMonth() ¶ public
isLastMonth(DateTimeZone|string|null $timezone = null): bool
确定实例是否在过去一个月内
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
boolisLastWeek() ¶ public
isLastWeek(DateTimeZone|string|null $timezone = null): bool
确定实例是否在过去一周内
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
boolisLastYear() ¶ public
isLastYear(DateTimeZone|string|null $timezone = null): bool
确定实例是否在过去一年内
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
boolisNextMonth() ¶ public
isNextMonth(DateTimeZone|string|null $timezone = null): bool
确定实例是否在下个月内
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
boolisNextWeek() ¶ public
isNextWeek(DateTimeZone|string|null $timezone = null): bool
确定实例是否在下周内
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
boolisNextYear() ¶ public
isNextYear(DateTimeZone|string|null $timezone = null): bool
确定实例是否在来年内
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
boolisPast() ¶ public
isPast(DateTimeZone|string|null $timezone = null): bool
确定实例是否在过去,即小于(早于)现在
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
boolisToday() ¶ public
isToday(DateTimeZone|string|null $timezone = null): bool
确定实例是否为今天
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
boolisTomorrow() ¶ public
isTomorrow(DateTimeZone|string|null $timezone = null): bool
确定实例是否为明天
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
boolisWithinNext() ¶ public
isWithinNext(string|int $timeInterval): bool
如果此实例将在指定的时间间隔内发生,则返回 true
参数
-
string|int$timeInterval 带有空格和时间类型的数值。有效类型的示例:6 小时、2 天、1 分钟。
返回
boolisYesterday() ¶ public
isYesterday(DateTimeZone|string|null $timezone = null): bool
确定实例是否为昨天
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
boollastOfMonth() ¶ public
lastOfMonth(int|null $dayOfWeek = null): static
修改为当前月份中给定一周的最后一次出现。如果没有提供 dayOfWeek,则修改为当前月份的最后一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
参数
-
int|null$dayOfWeek optional 要移动到的星期几。
返回
staticlastOfQuarter() ¶ public
lastOfQuarter(int|null $dayOfWeek = null): static
修改为当前季度中给定一周的最后一次出现。如果没有提供 dayOfWeek,则修改为当前季度的最后一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
参数
-
int|null$dayOfWeek optional 要移动到的星期几。
返回
staticlastOfYear() ¶ public
lastOfYear(int|null $dayOfWeek = null): static
修改为当前年份中给定一周的最后一次出现。如果没有提供 dayOfWeek,则修改为当前年份的最后一天。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
参数
-
int|null$dayOfWeek optional 要移动到的星期几。
返回
staticlessThan() ¶ public
lessThan(Cake\Chronos\ChronosDate $other): bool
确定实例是否小于(早于)另一个实例
参数
-
Cake\Chronos\ChronosDate$other 要比较的实例。
返回
boollessThanOrEquals() ¶ public
lessThanOrEquals(Cake\Chronos\ChronosDate $other): bool
确定实例是否小于(早于)或等于另一个实例
参数
-
Cake\Chronos\ChronosDate$other 要比较的实例。
返回
boolmodify() ¶ public
modify(string $modifier): static
创建一个新的实例,其日期根据 DateTimeImmutable::modifier() 修改。
尝试更改时间组件将引发异常。
参数
-
string$modifier 日期修饰符。
返回
staticnext() ¶ public
next(int|null $dayOfWeek = null): static
修改为给定一周的下一个出现。如果没有提供 dayOfWeek,则修改为当前一周的下一个出现。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
参数
-
int|null$dayOfWeek optional 要移动到的星期几。
返回
staticnice() ¶ public
nice(string|null $locale = null): string
返回此对象的格式良好的日期字符串。
要使用的格式存储在静态属性 Date::$niceFormat 中。
参数
-
string|null$locale optional 应以其显示日期的区域设置名称(例如 pt-BR)。
返回
字符串notEquals() ¶ public
notEquals(Cake\Chronos\ChronosDate $other): bool
确定实例是否不等于另一个实例
参数
-
Cake\Chronos\ChronosDate$other 要比较的实例。
返回
boolnow() ¶ public static
now(DateTimeZone|string|null $timezone = null): static
获取今天的日期。
参数
-
DateTimeZone|string|null$timezone optional 用于现在的时区。
返回
staticnthOfMonth() ¶ public
nthOfMonth(int $nth, int $dayOfWeek): static|false
修改为当前月份中给定一周的给定出现。如果计算出的出现超出当前月份的范围,则返回 false,并且不会进行任何修改。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
参数
-
int$nth 要使用的偏移量。
-
int$dayOfWeek 要移动到的星期几。
返回
static|falsenthOfQuarter() ¶ public
nthOfQuarter(int $nth, int $dayOfWeek): static|false
修改为当前季度中给定一周的给定出现。如果计算出的出现超出当前季度的范围,则返回 false,并且不会进行任何修改。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
参数
-
int$nth 要使用的偏移量。
-
int$dayOfWeek 要移动到的星期几。
返回
static|falsenthOfYear() ¶ public
nthOfYear(int $nth, int $dayOfWeek): static|false
修改为当前年份中给定一周的给定出现。如果计算出的出现超出当前年份的范围,则返回 false,并且不会进行任何修改。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
参数
-
int$nth 要使用的偏移量。
-
int$dayOfWeek 要移动到的星期几。
返回
static|falseparse() ¶ public static
parse(Cake\Chronos\ChronosDateDateTimeInterface|string $time): static
从字符串创建实例。这是构造函数的别名,它允许更好的流畅语法,因为它允许你执行 Chronos::parse('Monday next week')->fn() 而不是 (new Chronos('Monday next week'))->fn()
参数
-
Cake\Chronos\ChronosDateDateTimeInterface|string$time 要解析的 strtotime 兼容字符串。
返回
staticparseDate() ¶ public static
parseDate(string $date, string|int|null $format = null): static|null
根据传递的或配置的格式解析提供的 $date 字符串后,返回一个新的 Date 对象。此方法依赖于区域设置,传递给此函数的任何字符串都将被解释为依赖于区域设置的字符串。
如果没有提供 $format,则将使用 wordFormat 格式。
如果无法解析提供的日期,则将返回 null。
示例
$time = Date::parseDate('10/13/2013');
$time = Date::parseDate('13 Oct, 2013', 'dd MMM, y');
$time = Date::parseDate('13 Oct, 2013', IntlDateFormatter::SHORT);
参数
-
string$date 要解析的日期字符串。
-
string|int|null$format optional IntlDateFormatter 接受的任何格式。
返回
static|nullprevious() ¶ public
previous(int|null $dayOfWeek = null): static
修改为给定一周的先前出现。如果没有提供 dayOfWeek,则修改为当前一周的先前出现。使用提供的常量来指示所需的 dayOfWeek,例如 Chronos::MONDAY。
参数
-
int|null$dayOfWeek optional 要移动到的星期几。
返回
staticresetToStringFormat() ¶ public static
resetToStringFormat(): void
将 __toString() 格式重置为 DEFAULT_TO_STRING_FORMAT。
返回
voidsetDate() ¶ public
setDate(int $year, int $month, int $day): static
设置日期。
参数
-
int$year 要设置的年份。
-
int$month 要设置的月份。
-
int$day 要设置的日期。
返回
staticsetISODate() ¶ public
setISODate(int $year, int $week, int $dayOfWeek = 1): static
根据 ISO 8601 标准设置日期
参数
-
int$year 日期的年份。
-
int$week 日期的星期数。
-
int$dayOfWeek optional 从一周的第一天开始的偏移量。
返回
staticsetJsonEncodeFormat() ¶ public static
setJsonEncodeFormat(Closure|string|int $format): void
设置将此对象转换为 JSON 时使用的默认格式
格式应为 IntlDateFormatter 中的格式常量(如 https://secure.php.net/manual/en/class.intldateformatter.php 中所述)或 http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details 中指定的模式。
或者,格式可以提供回调函数。在这种情况下,回调函数可以接收此对象并返回格式化的字符串。
参数
-
Closure|string|int$format 格式。
返回
void另请参见
setToStringFormat() ¶ public static
setToStringFormat(string|int $format): void
设置将此类型实例类型转换为字符串时使用的默认格式
格式应为 IntlDateFormatter 中的格式常量(如 https://secure.php.net/manual/en/class.intldateformatter.php 中所述)或 https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classSimpleDateFormat.html#details 中指定的模式。
参数
-
string|int$format 格式。
返回
voidsub() ¶ public
sub(DateInterval $interval): static
从日期中减去一个时间间隔。
任何对时间更改都将被忽略并重置为 00:00:00
参数
-
DateInterval$interval 要修改此日期的时间间隔。
返回
staticsubMonths() ¶ public
subMonths(int $value): static
从实例中删除月份
与 addMonths() 的行为相同。
参数
-
int$value 要删除的月数。
返回
staticsubMonthsWithOverflow() ¶ public
subMonthsWithOverflow(int $value): static
将月数(含溢出)添加到实例。正 $value 前移,负 $value 后移。
如果新的 ChronosDate 不存在,则日期溢出到下个月。
示例
(new Chronos('2012-01-30'))->addMonthsWithOverflow(1); // Results in 2013-03-01
参数
-
int$value 要删除的月数。
返回
staticsubYears() ¶ public
subYears(int $value): static
从实例中删除年数。
与 addYears() 的行为相同。
参数
-
int$value 要移除的年数。
返回
staticsubYearsWithOverflow() ¶ public
subYearsWithOverflow(int $value): static
从实例中删除带溢出的年数
与 addYeasrWithOverflow() 的行为相同。
参数
-
int$value 要移除的年数。
返回
statictimeAgoInWords() ¶ public
timeAgoInWords(array<string, mixed> $options = []): string
根据当前日期和此对象之间的差异返回相对日期或格式化的绝对日期。
选项
from=> 另一个 Date 对象,表示“现在”日期format=> 如果相对时间超过 end 指定的时长,则使用回退格式accuracy=> 指定日期描述的准确性(数组)- year => 如果年数 > 0,则使用此格式(默认“day”)
- month => 如果月数 > 0,则使用此格式(默认“day”)
- week => 如果周数 > 0,则使用此格式(默认“day”)
- day => 如果周数 > 0,则使用此格式(默认“day”)
end=> 相对日期说明的结束relativeString=> 输出相对日期时使用的 printf 兼容字符串absoluteString=> 输出绝对日期时使用的 printf 兼容字符串timezone=> 时间戳应以其格式化的用户时区。
相对日期看起来像这样
- 3 周前,4 天前
- 1 天前
默认日期格式为 d/M/YY,例如:09 年 2 月 18 日。格式化在内部使用 i18nFormat 进行,有关有效格式化字符串,请参阅该方法。
返回的字符串包含“ago”或“on”,并假定您会在函数输出之前正确添加诸如“Posted”之类的词语。
注意:如果差异为一周或更长时间,则最低级别的准确性为 day。
参数
-
array<string, mixed>$options 可选 选项数组。
返回
字符串toDateTimeImmutable() ¶ public
toDateTimeImmutable(DateTimeZone|string|null $timezone = null): DateTimeImmutable
以午夜的 DateTimeImmutable 实例的形式返回日期。
参数
-
DateTimeZone|string|null$timezone optional DateTimeImmutable 实例所在的时区
返回
DateTimeImmutabletoNative() ¶ public
toNative(DateTimeZone|string|null $timezone = null): DateTimeImmutable
以午夜的 DateTimeImmutable 实例的形式返回日期。
toDateTimeImmutable() 的别名。
参数
-
DateTimeZone|string|null$timezone optional DateTimeImmutable 实例所在的时区
返回
DateTimeImmutabletoQuarter() ¶ public
toQuarter(bool $range = false): array|int
返回季度。
参数
-
bool$range 可选 范围。
返回
array|inttoRfc1036String() ¶ public
toRfc1036String(): string
将实例格式化为 RFC1036。
返回
字符串链接
toRfc1123String() ¶ public
toRfc1123String(): string
将实例格式化为 RFC1123。
返回
字符串链接
toRfc2822String() ¶ public
toRfc2822String(): string
将实例格式化为 RFC2822。
返回
字符串链接
toRfc3339String() ¶ public
toRfc3339String(): string
将实例格式化为 RFC3339。
返回
字符串链接
toRfc822String() ¶ public
toRfc822String(): string
将实例格式化为 RFC822。
返回
字符串链接
toRfc850String() ¶ public
toRfc850String(): string
将实例格式化为 RFC850。
返回
字符串链接
today() ¶ public static
today(DateTimeZone|string|null $timezone = null): static
获取今天的日期。
参数
-
DateTimeZone|string|null$timezone optional 用于今天的时区。
返回
statictomorrow() ¶ public static
tomorrow(DateTimeZone|string|null $timezone = null): static
获取明天的日期。
参数
-
DateTimeZone|string|null$timezone optional 用于明天的时区。
返回
staticwasWithinLast() ¶ public
wasWithinLast(string|int $timeInterval): bool
如果此实例发生在指定的时间间隔内,则返回 true。
参数
-
string|int$timeInterval 带有空格和时间类型的数值。有效类型的示例:6 小时、2 天、1 分钟。
返回
boolyesterday() ¶ public static
yesterday(DateTimeZone|string|null $timezone = null): static
获取昨天的日期。
参数
-
DateTimeZone|string|null$timezone optional 用于昨天的时区。
返回
static属性详细
$_jsonEncodeFormat ¶ protected static
将此对象转换为 JSON 时使用的格式。
格式应为 IntlDateFormatter 中的格式常量(如 https://secure.php.net/manual/en/class.intldateformatter.php 中所述)或 https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classSimpleDateFormat.html#details 中指定的模式。
类型
Closure|string|int$_toStringFormat ¶ protected static
使用 Cake\I18n\Date::i18nFormat() 和 __toString 格式化时间时使用的格式。
格式应为 IntlDateFormatter 中的格式常量(如 https://secure.php.net/manual/en/class.intldateformatter.php 中所述)或 https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classSimpleDateFormat.html#details 中指定的模式。
类型
string|int$niceFormat ¶ public static
使用 Cake\I18n\Date::nice() 格式化时间时使用的格式
格式应为 IntlDateFormatter 中的格式常量(如 https://secure.php.net/manual/en/class.intldateformatter.php 中所述)或 https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classSimpleDateFormat.html#details 中指定的模式。
类型
string|int$wordAccuracy ¶ public static
使用 Date::timeAgoInWords() 格式化时间且差异小于 Date::$wordEnd 时使用的格式
类型
array<string, string>$wordFormat ¶ public static
使用 Cake\I18n\Date::timeAgoInWords() 格式化时间且差异大于 Cake\I18n\Date::$wordEnd 时使用的格式
类型
string|int