Matt Elhotiby
Matt Elhotiby

Reputation: 44066

PHP parse a string to make a date?

Ok i have this in my mysql database in a datetime field

2011-12-30 14:07:46

i need using php to use this format

December 30, 2011, 2:07 pm

so i used this

date("F j, Y, g:i a", $system['date_added']);

but the outcome was this error

A non well formed numeric value encountered in quote.php on line 56

and this outcome

December 31, 1969, 4:33 pm

I know there is

print_r(date_parse($system['date_added']));

but i get an array....any ideas on an easy way to address this

Upvotes: 1

Views: 1112

Answers (4)

kba
kba

Reputation: 19466

The date()'s second argument is a UNIX timestamp, you're giving it 2011-12-30 14:07:46 which is not.

There are two ways to handle this. Either one will work.

  • Pull out the date as a UNIX timestamp.

    SELECT UNIX_TIMESTAMP(`field`) FROM ...
    
  • Parse the date using strtotime().

    $timestamp = strtotime($system['date_added']);
    date("F j, Y, g:i a", $timestamp);
    

Upvotes: 2

Teson
Teson

Reputation: 6736

wrap your db-date with strtotime()

http://php.net/manual/en/function.strtotime.php

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227180

You can use strtotime.

date("F j, Y, g:i a", strtotime($system['date_added']));

Upvotes: 3

Headshota
Headshota

Reputation: 21449

The second param of the date should be the unix timestamp, which you can get using strtotime which gets string representation of a date and converts it to unix timestamp:

date("F j, Y, g:i a", strtotime($system['date_added']));

Upvotes: 3

Related Questions