日期辅助函数

日期辅助函数文件包含协助处理日期的函数。

备注

许多之前在 CodeIgniter 3 date_helper 中的函数已经移动到 CodeIgniter 4 的 Time 类中。

加载此辅助函数

使用以下代码加载此辅助函数:

<?php

helper('date');

可用函数

提供以下函数:

now([$timezone = null])
参数:
  • $timezone (string) -- 时区

返回:

UNIX 时间戳

返回类型:

int

备注

推荐改为使用 Time 类。使用 Time::now()->getTimestamp() 获取当前 UNIX 时间戳。

如果没有提供时区,将通过 time() 返回当前 UNIX 时间戳。

<?php

echo now();

如果提供了任何 PHP 支持的时区,它将返回一个根据时差偏移后的时间戳。这与当前的(标准)UNIX 时间戳不同。

如果你不打算将主时间基准设置为其他 PHP 支持的时区(通常只有在运行允许用户自定义时区的网站时才会这样做),那么使用此函数相比 PHP 原生的 time() 函数并没有任何优势。

timezone_select([$class = '', $default = '', $what = \DateTimeZone::ALL, $country = null])
参数:
  • $class (string) -- 应用于选择字段的可选类

  • $default (string) -- 初始选择的默认值

  • $what (int) -- DateTimeZone 类常量(参见 listIdentifiers

  • $country (string) --

    两位字母的 ISO 3166-1 兼容的国家代码(参见 listIdentifiers

返回:

预格式化的 HTML 选择字段

返回类型:

string

生成一个包含可用时区的 select 表单字段(即下拉菜单,可根据 $what$country 参数进行筛选)。 你可以提供一个 CSS 类应用到该字段以简化样式设置,同时也可以指定一个默认选中值。

<?php

echo timezone_select('custom-select', 'America/New_York');