kevi kevi
kevi kevi

Reputation: 165

Why is strtotime not working?

My content array contains a date field that I'm trying to format as "F Y".

When I print_r, I'll get this:

Array
(
    [title] => Test
    [field_datetime_value_1] => 2012-01-16
    [field_datetime_value2] => 2012-01-20
)

If I try:

$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;

I get this:

Test 1:
Test 2: December 1969
Test 3:2012-01-16

I guess I was expecting that in the case of Test 2, I'd get what I was after (namely, January 2012). Can someone help me out here? What am I missing?

Upvotes: 0

Views: 974

Answers (2)

johann
johann

Reputation: 1135

<?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;

?>

Result :

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

As Justin Lucas said, results are printed correctly.

"strtotime" method is available since old versions (php.net), It means that you have a problem in your array. Are you sure that array's content is not modified before the "test" variables definitions ?

Upvotes: 1

Justin Lucas
Justin Lucas

Reputation: 2321

It looks like the data in your array is corrupt. Try this:

$content = array(
    'title' => 'test',
    'field_datetime_value_1' => '2012-01-16',
    'field_datetime_value2' => '2012-01-20'
);

Upvotes: 1

Related Questions