0

我的内容数组包含一个我试图格式化为“F Y”的日期字段。

当我 print_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 月)。有人可以帮我吗?我错过了什么?

4

2 回答 2

1

看起来您的数组中的数据已损坏。试试这个:

$content = array(
    'title' => 'test',
    'field_datetime_value_1' => '2012-01-16',
    'field_datetime_value2' => '2012-01-20'
);
于 2011-12-02T01:23:55.480 回答
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

正如贾斯汀卢卡斯所说,结果打印正确。

“strtotime”方法从旧版本(php.net)开始可用,这意味着你的数组有问题。您确定在“测试”变量定义之前未修改数组的内容吗?

于 2011-12-02T01:49:10.767 回答