date_format with MySQL and PHP

date_format with MySQL and PHP

am 07.08.2006 14:17:20 von mpar612

Hello everyone,

I am having some difficulty with the date_format function in MySQL. I
have an 'add_date' column in my database as a timestamp type. This
automatically adds the current date and time when a user submits a
form. I am using PHP and trying to format the date.

I am using the following query:

SELECT isbn, artist_name, album_title, release_date,
date_format(\'add_date\', \'%M %d %Y\'), description, price FROM lounge

When I run this query my date returns null.

If I run the following query, the standard timestamp formatting is
returned:

SELECT isbn, artist_name, album_title, release_date, add_date,
description, price FROM lounge

Am I making an error with my query?

Thanks!

Re: date_format with MySQL and PHP

am 07.08.2006 14:54:14 von Rik

mpar612@gmail.com wrote:
> Hello everyone,
>
> I am having some difficulty with the date_format function in MySQL. I
> have an 'add_date' column in my database as a timestamp type. This
> automatically adds the current date and time when a user submits a
> form. I am using PHP and trying to format the date.
>
> I am using the following query:
>
> SELECT isbn, artist_name, album_title, release_date,
> date_format(\'add_date\', \'%M %d %Y\'), description, price FROM
> lounge
>
> When I run this query my date returns null.

That's correct, the string 'add_date' isn't a date. Use backticks (` instead
of ') around fieldnames, or they'll be interpreted as strings. In this case
the backticks are probably not even necessary:

"SELECT isbn, artist_name, album_title, release_date, date_format(add_date,
'%M %d %Y') as 'add_date', description, price FROM lounge"

Grtz,
--
Rik Wasmus

Re: date_format with MySQL and PHP

am 07.08.2006 15:00:51 von mpar612

Rik wrote:
> mpar612@gmail.com wrote:
> > Hello everyone,
> >
> > I am having some difficulty with the date_format function in MySQL. I
> > have an 'add_date' column in my database as a timestamp type. This
> > automatically adds the current date and time when a user submits a
> > form. I am using PHP and trying to format the date.
> >
> > I am using the following query:
> >
> > SELECT isbn, artist_name, album_title, release_date,
> > date_format(\'add_date\', \'%M %d %Y\'), description, price FROM
> > lounge
> >
> > When I run this query my date returns null.
>
> That's correct, the string 'add_date' isn't a date. Use backticks (` instead
> of ') around fieldnames, or they'll be interpreted as strings. In this case
> the backticks are probably not even necessary:
>
> "SELECT isbn, artist_name, album_title, release_date, date_format(add_date,
> '%M %d %Y') as 'add_date', description, price FROM lounge"
>
> Grtz,
> --
> Rik Wasmus

Excellent, works great now. Thank you!