CakePHP
  • 文档
    • 书籍
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识和商标
  • 商业解决方案
  • 商品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与
    • 问题 (Github)
    • 烘焙坊
    • 精选资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 时事通讯
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • 帮助和支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • 缓存
    • 集合
    • 命令
    • 控制台
    • 控制器
    • 核心
    • 数据库
    • 数据源
    • 错误
    • 事件
    • 表单
    • Http
    • 国际化
    • 日志
    • 邮件
    • 网络
    • ORM
    • 路由
    • 测试套件
    • 实用程序
    • 验证
    • 视图
      • 异常
      • 表单
      • 辅助函数
      • 小部件

类 TimeHelper

用于轻松使用时间数据的 Time Helper 类。

时间数据的操作。

命名空间: Cake\View\Helper
参见: \Cake\I18n\Time
链接: https://book.cakephp.com.cn/5/en/views/helpers/time.html

属性摘要

  • $_View protected
    Cake\View\View

    此辅助函数所附加到的视图实例

  • $_config protected
    array<string, mixed>

    运行时配置

  • $_configInitialized protected
    bool

    配置属性是否已使用默认值进行配置

  • $_defaultConfig protected
    array<string, mixed>

    配置选项

  • $_templater protected
    Cake\View\StringTemplate|null

    StringTemplate 实例。

  • $helperInstances protected
    array<string,Cake\View\Helper>

    加载的辅助函数实例。

  • $helpers protected
    array

    此辅助函数使用的辅助函数列表

方法摘要

  • __construct() public

    默认构造函数

  • __debugInfo() public

    返回一个数组,可用于描述此对象的内部状态。

  • __get() public

    延迟加载辅助函数。

  • _configDelete() protected

    删除单个配置键。

  • _configRead() protected

    读取配置键。

  • _configWrite() protected

    写入配置键。

  • _confirm() protected

    返回一个字符串,用作确认对话框的 onclick 处理程序。

  • _getTimezone() protected

    获取时区。

  • addClass() public

    将给定类添加到元素选项

  • configShallow() public

    将提供的配置与现有配置合并。与 config()(它对嵌套键进行递归合并)不同,此方法进行简单合并。

  • format() public

    返回格式化的日期字符串,给定 Time 实例、UNIX 时间戳或有效的 strtotime() 日期字符串。

  • formatTemplate() public

    使用 $data 格式化模板字符串

  • fromString() public

    返回 DateTime 对象,给定 UNIX 时间戳或有效的 strtotime() 日期字符串。

  • getConfig() public

    返回配置。

  • getConfigOrFail() public

    返回此特定键的配置。

  • getTemplates() public

    获取要使用的模板或特定模板。

  • getView() public

    获取此辅助函数绑定到的视图实例。

  • gmt() public

    返回作为 UNIX 时间戳的格林尼治标准时间。

  • i18nFormat() public

    返回格式化的日期字符串,给定 Datetime 实例、UNIX 时间戳或有效的 strtotime() 日期字符串。

  • implementedEvents() public

    事件监听器。

  • initialize() public

    构造函数钩子方法。

  • isFuture() public

    如果给定的日期时间字符串在将来,则返回 true。

  • isPast() public

    如果给定的日期时间字符串在过去,则返回 true。

  • isThisMonth() public

    如果给定的日期时间字符串在本月内,则返回 true

  • isThisWeek() public

    如果给定的日期时间字符串在本周内,则返回 true。

  • isThisYear() public

    如果给定的日期时间字符串在今年内,则返回 true。

  • isToday() public

    如果给定的日期时间字符串是今天,则返回 true。

  • isTomorrow() public

    如果给定的日期时间字符串是明天,则返回 true。

  • isWithinNext() public

    如果指定的日期时间在指定的间隔内,则返回 true,否则返回 false。

  • nice() public

    对于给定的日期时间字符串返回格式良好的日期字符串。

  • setConfig() public

    设置配置。

  • setTemplates() public

    设置要使用的模板。

  • templater() public

    返回模板器实例。

  • timeAgoInWords() public

    将日期格式化为表示相对时间的短语。

  • toAtom() public

    返回为 Atom RSS 提要格式化的日期。

  • toQuarter() public

    返回季度

  • toRss() public

    为 RSS 提要格式化日期

  • toUnix() public

    从文本日期时间描述返回 UNIX 时间戳。

  • wasWithinLast() public

    如果指定的日期时间在指定的间隔内,则返回 true,否则返回 false。

  • wasYesterday() public

    如果给定的日期时间字符串是昨天,则返回 true。

方法详情

__construct() ¶ public

__construct(Cake\View\View $view, array<string, mixed> $config = [])

默认构造函数

参数
Cake\View\View $view

此辅助函数所附加到的视图。

array<string, mixed> $config optional

辅助函数的配置设置。

__debugInfo() ¶ public

__debugInfo(): array<string, mixed>

返回一个数组,可用于描述此对象的内部状态。

返回
array<string, mixed>

__get() ¶ public

__get(string $name): Cake\View\Helper|null

延迟加载辅助函数。

参数
string $name

正在访问的属性的名称。

返回
Cake\View\Helper|null

_configDelete() ¶ protected

_configDelete(string $key): void

删除单个配置键。

参数
string $key

要删除的键。

返回
void
抛出
Cake\Core\Exception\CakeException
如果尝试覆盖现有配置

_configRead() ¶ protected

_configRead(string|null $key): mixed

读取配置键。

参数
string|null $key

要读取的键。

返回
mixed

_configWrite() ¶ protected

_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void

写入配置键。

参数
array<string, mixed>|string $key

要写入的键。

mixed $value

要写入的值。

string|bool $merge optional

True 表示递归合并,'shallow' 表示简单合并,false 表示覆盖,默认为 false。

返回
void
抛出
Cake\Core\Exception\CakeException
如果尝试覆盖现有配置

_confirm() ¶ protected

_confirm(string $okCode, string $cancelCode): string

返回一个字符串,用作确认对话框的 onclick 处理程序。

参数
string $okCode

用户选择“确定”后要执行的代码

string $cancelCode

用户选择“取消”后要执行的代码

返回
string

_getTimezone() ¶ protected

_getTimezone(DateTimeZone|string|null $timezone): DateTimeZone|string|null

获取时区。

将使用提供的时区,或如果已定义,则使用默认输出时区。

参数
DateTimeZone|string|null $timezone

如果适用,则覆盖时区。

返回
DateTimeZone|string|null

addClass() ¶ public

addClass(array<string, mixed> $options, string $class, string $key = 'class'): array<string, mixed>

将给定类添加到元素选项

参数
array<string, mixed> $options

要添加类的数组选项/属性

string $class

正在添加的类名。

string $key optional

要用于类的键。默认为 'class'。

返回
array<string, mixed>

configShallow() ¶ public

configShallow(array<string, mixed>|string $key, mixed|null $value = null): $this

将提供的配置与现有配置合并。与 config()(它对嵌套键进行递归合并)不同,此方法进行简单合并。

设置特定值

$this->configShallow('key', $value);

设置嵌套值

$this->configShallow('some.nested.key', $value);

同时更新多个配置设置

$this->configShallow(['one' => 'value', 'another' => 'value']);
参数
array<string, mixed>|string $key

要设置的键,或配置的完整数组。

mixed|null $value optional

要设置的值。

返回
$this

format() ¶ public

format(Cake\Chronos\ChronosDateDateTimeInterface|string|int|null $date, array<int>|string|int|null $format = null, string|false $invalid = false, DateTimeZone|string|null $timezone = null): string|int|false

返回格式化的日期字符串,给定 Time 实例、UNIX 时间戳或有效的 strtotime() 日期字符串。

此方法是 TimeHelper::i18nFormat() 的别名。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int|null $date

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象(或日期格式字符串)。

array<int>|string|int|null $format optional

日期格式字符串(或 UNIX 时间戳、strtotime() 有效字符串或 DateTime 对象)。

string|false $invalid optional

无效日期显示的默认值

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
string|int|false
另请参阅
\Cake\I18n\Time::i18nFormat()

formatTemplate() ¶ public

formatTemplate(string $name, array<string, mixed> $data): string

使用 $data 格式化模板字符串

参数
string $name

模板名称。

array<string, mixed> $data

要插入的数据。

返回
string

fromString() ¶ public

fromString(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): Cake\I18n\DateTime

返回 DateTime 对象,给定 UNIX 时间戳或有效的 strtotime() 日期字符串。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
Cake\I18n\DateTime

getConfig() ¶ public

getConfig(string|null $key = null, mixed $default = null): mixed

返回配置。

用法

读取整个配置

$this->getConfig();

读取特定值

$this->getConfig('key');

读取嵌套值

$this->getConfig('some.nested.key');

读取默认值

$this->getConfig('some-key', 'default-value');
参数
string|null $key optional

要获取的键,如果要获取整个配置,则为 null。

mixed $default optional

键不存在时的返回值。

返回
mixed

getConfigOrFail() ¶ public

getConfigOrFail(string $key): mixed

返回此特定键的配置。

此键的配置值必须存在,不能为 null。

参数
string $key

要获取的键。

返回
mixed
抛出
InvalidArgumentException

getTemplates() ¶ public

getTemplates(string|null $template = null): array|string

获取要使用的模板或特定模板。

参数
string|null $template optional

用于读取特定模板的字符串,如果要获取所有模板,则为 null。

返回
array|string

getView() ¶ public

getView(): Cake\View\View

获取此辅助函数绑定到的视图实例。

返回
Cake\View\View

gmt() ¶ public

gmt(Cake\Chronos\ChronosDateDateTimeInterface|string|int|null $string = null): string

返回作为 UNIX 时间戳的格林尼治标准时间。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int|null $string optional

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

返回
string
另请参阅
\Cake\I18n\Time::gmt()

i18nFormat() ¶ public

i18nFormat(Cake\Chronos\ChronosDateDateTimeInterface|string|int|null $date, array|string|int|null $format = null, string|false $invalid = false, DateTimeZone|string|null $timezone = null): string|int|false

返回格式化的日期字符串,给定 Datetime 实例、UNIX 时间戳或有效的 strtotime() 日期字符串。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int|null $date

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

array|string|int|null $format optional

Intl 兼容的格式字符串。

string|false $invalid optional

无效日期显示的默认值

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
string|int|false
抛出
异常
日期无法解析时
另请参阅
\Cake\I18n\Time::i18nFormat()

implementedEvents() ¶ public

implementedEvents(): array<string, mixed>

事件监听器。

通过定义其中一个回调方法,假定助手对相关事件感兴趣。

如果您需要添加非传统事件监听器,或者您希望助手监听非标准事件,请覆盖此方法。

返回
array<string, mixed>

initialize() ¶ public

initialize(array<string, mixed> $config): void

构造函数钩子方法。

实现此方法以避免覆盖构造函数并调用父类。

参数
array<string, mixed> $config

提供给此助手的配置设置。

返回
void

isFuture() ¶ public

isFuture(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): bool

如果给定的日期时间字符串在将来,则返回 true。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
bool

isPast() ¶ public

isPast(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): bool

如果给定的日期时间字符串在过去,则返回 true。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
bool

isThisMonth() ¶ public

isThisMonth(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): bool

如果给定的日期时间字符串在本月内,则返回 true

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
bool

isThisWeek() ¶ public

isThisWeek(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): bool

如果给定的日期时间字符串在本周内,则返回 true。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
bool

isThisYear() ¶ public

isThisYear(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): bool

如果给定的日期时间字符串在今年内,则返回 true。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
bool

isToday() ¶ public

isToday(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): bool

如果给定的日期时间字符串是今天,则返回 true。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
bool

isTomorrow() ¶ public

isTomorrow(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): bool

如果给定的日期时间字符串是明天,则返回 true。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
bool

isWithinNext() ¶ public

isWithinNext(string $timeInterval, Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): bool

如果指定的日期时间在指定的间隔内,则返回 true,否则返回 false。

参数
string $timeInterval

带有空格的数字值,然后是时间类型。有效类型的示例:6 小时、2 天、1 分钟。

Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
bool
另请参阅
\Cake\I18n\Time::wasWithinLast()

nice() ¶ public

nice(DateTimeInterface|string|int|null $dateString = null, DateTimeZone|string|null $timezone = null, string|null $locale = null): string

对于给定的日期时间字符串返回格式良好的日期字符串。

参数
DateTimeInterface|string|int|null $dateString optional

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

string|null $locale optional

语言环境字符串。

返回
string

setConfig() ¶ public

setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this

设置配置。

用法

设置特定值

$this->setConfig('key', $value);

设置嵌套值

$this->setConfig('some.nested.key', $value);

同时更新多个配置设置

$this->setConfig(['one' => 'value', 'another' => 'value']);
参数
array<string, mixed>|string $key

要设置的键,或配置的完整数组。

mixed|null $value optional

要设置的值。

bool $merge optional

是否递归合并或覆盖现有配置,默认为 true。

返回
$this
抛出
Cake\Core\Exception\CakeException
尝试设置无效键时。

setTemplates() ¶ public

setTemplates(array<string, string> $templates): $this

设置要使用的模板。

参数
array<string, string> $templates

要添加的模板。

返回
$this

templater() ¶ public

templater(): Cake\View\StringTemplate

返回模板器实例。

返回
Cake\View\StringTemplate

timeAgoInWords() ¶ public

timeAgoInWords(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateTime, array<string, mixed> $options = []): string

将日期格式化为表示相对时间的短语。

其他选项

  • element - 用于包装格式化时间的元素。有一些额外的选项
    • tag - 要使用的标签,默认为 'span'。
    • class - 要使用的类名,默认为 time-ago-in-words。
    • title - 默认为 $dateTime 输入。
参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateTime

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象。

array<string, mixed> $options optional

如果 $dateString 中使用时间戳,则使用默认格式

返回
string
另请参阅
\Cake\I18n\Time::timeAgoInWords()

toAtom() ¶ public

toAtom(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): string

返回为 Atom RSS 提要格式化的日期。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
string
另请参阅
\Cake\I18n\Time::toAtom()

toQuarter() ¶ public

toQuarter(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, bool $range = false): list<string>|int

返回季度

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

bool $range optional

如果为 true,则返回 Y-m-d 格式的范围

返回
list<string>|int
另请参阅
\Cake\I18n\Time::toQuarter()

toRss() ¶ public

toRss(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): string

为 RSS 提要格式化日期

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
string

toUnix() ¶ public

toUnix(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): string

从文本日期时间描述返回 UNIX 时间戳。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
string
另请参阅
\Cake\I18n\Time::toUnix()

wasWithinLast() ¶ public

wasWithinLast(string $timeInterval, Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): bool

如果指定的日期时间在指定的间隔内,则返回 true,否则返回 false。

参数
string $timeInterval

带有空格的数字值,然后是时间类型。有效类型的示例:6 小时、2 天、1 分钟。

Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
bool
另请参阅
\Cake\I18n\Time::wasWithinLast()

wasYesterday() ¶ public

wasYesterday(Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString, DateTimeZone|string|null $timezone = null): bool

如果给定的日期时间字符串是昨天,则返回 true。

参数
Cake\Chronos\ChronosDateDateTimeInterface|string|int $dateString

UNIX 时间戳,strtotime() 有效字符串或 DateTime 对象

DateTimeZone|string|null $timezone optional

用户时区字符串或 DateTimeZone 对象

返回
bool

属性详细信息

$_View ¶ protected

此辅助函数所附加到的视图实例

类型
Cake\View\View

$_config ¶ protected

运行时配置

类型
array<string, mixed>

$_configInitialized ¶ protected

配置属性是否已使用默认值进行配置

类型
bool

$_defaultConfig ¶ 受保护的

配置选项

类型
array<string, mixed>

$_templater ¶ 受保护的

StringTemplate 实例。

类型
Cake\View\StringTemplate|null

$helperInstances ¶ 受保护的

加载的辅助函数实例。

类型
array<string,Cake\View\Helper>

$helpers ¶ 受保护的

此辅助函数使用的辅助函数列表

类型
array
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 书籍
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识和商标
  • 社区
  • 参与
  • 问题 (Github)
  • 烘焙坊
  • 精选资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 时事通讯
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助和支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成