php - PHP DateTime::__construct():无法解析位置 8 (0) 的时间字符串:意外字符
问题描述
DateTime
使用课程时出现以下错误
DateTime::__construct():无法在位置 8 (0) 解析时间字符串 (1607990400):意外字符
代码
// post meta returns below value
// 1607990400
$wp_timezone = get_option('timezone_string');
$timezone = $wp_timezone ? $wp_timezone : 'UTC';
new DateTime(get_post_meta($group_id, 'last_date', TRUE), new DateTimeZone($timezone));
解决方案
DateTime
不期望在构建时有一个 Unix 时间戳。所以你必须使用createFromFormat
方法:
$dt = DateTime::createFromFormat(
'U',
get_post_meta($group_id, 'last_date', TRUE),
new DateTimeZone($timezone)
);
推荐阅读
- python - 如何在不使用 python 中的任何库函数的情况下获取字符的 ASCII 值?
- javascript - 如何比较两个对象数组并将新键添加到第二个数组中的对象
- python - 向列名添加特殊字符
- c++ - Windows 凭据提供程序日志记录
- javascript - 为什么使用分页后我不能得到排序结果?
- python - 如何在 python setuptools 中隐藏包(但仍然使用它们)
- winforms - 如何使用 Graph API 在 ListView 中显示频道?
- android - 应用退出时如何停止网络回调?
- azure - .Net Core API 的 oAuth 实现并将其部署在应用服务和 APIM 中
- reactjs - 如何在 cPanel 肝脏服务器中编辑我的 React 应用程序?