Page 1 of 1

Date and time

Posted: Sat Sep 17, 2005 1:58 pm
by vchris
I am trying to display the time like this: "1:05 pm". But it seems php can't get the "pm" and the 1 digit hour.

Here is what I got:

Code: Select all

$query = "SELECT newsid, newsheadline, newscontent, DATE_FORMAT(newsdate, '%M %d, %Y %g:%i %a') AS nd FROM news ORDER BY newsdate DESC";
%g should get the 1 digit hour and %a the pm or am.

This is what I have in the database:
9/15/2005 5:07:10 PM

This is what I have displayed:
September 15, 2005 g:54 Thu

Posted: Sat Sep 17, 2005 2:03 pm
by feyd
you're assuming MySQL supports the same format parameters as PHP's date() function, which it does not.

Have a read through the DATE_FORMAT() section here: http://dev.mysql.com/doc/mysql/en/date- ... tions.html

Posted: Sat Sep 17, 2005 2:44 pm
by vchris
Thanx I got it now.

How would I insert a certain date with mysql depending on the date the user choosed in drop-downs?

Posted: Sat Sep 17, 2005 2:59 pm
by feyd
format it to MySQL's format for the column type or convert it to a unix timestamp and pass that to MySQL's FROM_UNIXTIME() function.

Posted: Sat Sep 17, 2005 3:12 pm
by vchris
I'm able to format the date but only thing is how can I have the AM or PM added at the end? is it even possible or would I have the user choose times from 1 to 24?

Posted: Sat Sep 17, 2005 4:44 pm
by feyd
there are several possibilities:
  • 0-23, 1-24
  • 12AM-11PM
  • 1-12 with a radio, checkbox, combo selector