我的内容数组包含一个日期字段,我正在尝试将其格式化为“F Y”。
当我打印_r时,我会得到这个:
Array
(
[title] => Test
[field_datetime_value_1] => 2012-01-16
[field_datetime_value2] => 2012-01-20
)
如果我尝试:
$test1 = date("F Y", $content['field_datetime_value_1']);
$test2 = date("F Y", strtotime($content['field_datetime_value_1']));
$test3 = $content['field_datetime_value_1'];
print 'Test 1: '.$test1.'<br />Test 2: '.$test2.'<br />Test 3:'.$test3;
我明白了:
Test 1:
Test 2: December 1969
Test 3:2012-01-16
我想我期待在测试2的情况下,我会得到我想要的东西(即2012年1月)。有人可以帮帮我吗?我错过了什么?
答案 0 :(得分:1)
看起来数组中的数据已损坏。试试这个:
$content = array(
'title' => 'test',
'field_datetime_value_1' => '2012-01-16',
'field_datetime_value2' => '2012-01-20'
);
答案 1 :(得分:1)
<?php
//your array
$content = array (
'title' => 'Test',
'field_datetime_value_1' => '2012-01-16',
'field_datetime_value2' => '2012-01-20'
);
//debug your array
echo "<pre>";
var_dump($content);
echo "</pre>";
$test1 = date("F Y", $content['field_datetime_value_1']);
$test2 = date("F Y", strtotime($content['field_datetime_value_1']));
$test3 = $content['field_datetime_value_1'];
print 'Test 1: '.$test1.'<br />Test 2: '.$test2.'<br />Test 3: '.$test3;
?>
结果:
array(3) {
["title"]=>
string(4) "Test"
["field_datetime_value_1"]=>
string(10) "2012-01-16"
["field_datetime_value2"]=>
string(10) "2012-01-20"
}
Test 1: January 1970
Test 2: January 2012
Test 3: 2012-01-16
正如Justin Lucas所说,结果打印正确。
“strtotime”方法可用于旧版本(php.net),这意味着您的阵列中存在问题。你确定在“测试”变量定义之前没有修改数组的内容吗?