Harsha M V
Harsha M V

Reputation: 54989

PHP date format error

I have an array with a key timestamp with the following content

"timestamp" => "2011-11-29 00:00:00"

When i try to change the format using this

date("F j, Y", $data['Visitor']['timestamp']);

i get the following error

A non well formed numeric value encountered

Upvotes: 0

Views: 761

Answers (3)

John Conde
John Conde

Reputation: 219934

A newer way to do this as of PHP 5.2 is the DateTime class:

$datetime = new DateTime('2011-11-29 00:00:00');
echo $datetime->format('F j, Y');

See it in action

Upvotes: 0

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14951

The function requires the Unix Time which is numeric - and not a string formatted date.

As @Shakti Singh mentions you should use strtotime for that.

From the PHP docs on the timestamp parameter:

The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86476

You should be using the strtotime on the datetime data to convert it into Unix timestamp first.

date("F j, Y", strtotime($data['Visitor']['timestamp']));

Checkout the documentation of date it accept a Unix timestamp as a second parameter and you are passing a datetime value.

DEMO

Upvotes: 6

Related Questions