![](/img/trans.png)
[英]DateTime::__construct(): Failed to parse time string (//) at position 0 (/): Unexpected character
[英]PHP DateTime::__construct(): Failed to parse time string at position 8 (0): Unexpected character
使用DateTime
class 時出現以下錯誤
DateTime::__construct():無法在 position 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)
);
如果您將“@”與 Unix 時間戳連接,則 DateTime 可以處理此問題。
$getPostMeta = '1607990400'; //Unix-Timestamp
$dt = new DateTime('@'.$getPostMeta);
echo $dt->format('Y-m-d H:i'); //2020-12-15 00:00
時區始終為“+00:00”(UTC),如果時區設置為可選參數。 這被忽略了。
這也適用於 DateTime::createFromFormat ('U', ..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.