current_datetime()
云策文档标注
概述
current_datetime() 函数用于获取基于站点时区的当前时间,返回一个 DateTimeImmutable 对象。此函数在 WordPress 5.3.0 版本中引入。
关键要点
- 返回 DateTimeImmutable 对象,表示当前日期和时间
- 使用站点时区(通过 wp_timezone() 获取)
- 在 WordPress 5.3.0 版本中引入
代码示例
function current_datetime() {
return new DateTimeImmutable( 'now', wp_timezone() );
}注意事项
- 相关函数:wp_timezone() 用于获取站点时区对象
- 使用场景:例如在 wp_dashboard_recent_posts() 中生成发布相关内容
原文内容