PDA

View Full Version : Date in MySql and PHP


AryaputrA
06-12-2003, 03:08 PM
hi,

just say i need the date & timeto display as follows :

January 21 2003 5.03am

how do i set up a table in MySql, like the values for 'default' the VarChar or Timestamp. what do i need to enter for creating a row called date?

Next question, how do i use the insert command in php so that the dates are added automatically to the date row in MySql, and if im displaying the date value in a page, whats the command like to display the date as the sample above.

Please help i'm a newbie

mordred
06-12-2003, 09:12 PM
A question in return: Do you need to know how to change the table form the console or through an administration app, like phpMyAdmin? The latter is widely used and using it is very straightforward.

In your case, create a new column of the type DATETIME. That will store a human-readable date type which can be used by MySQL for date calculations.

About inserting a new date: Normally you have define explicitly which fields should be inserted. But it's fairly easy to do, so you only have extend your previous SQL statement in a fashion that the DATETIME field is set with NOW(). This stores the current date (obviously).

If you want to display a formatted version of the date field, you can use MySQLs built-in function DATE_FORMAT. It's a very flexible function which can produce a lot of different formats based on certain modifiers you pass in a format string to it. For your example, you could use


SELECT DATE_FORMAT(dateColumn, "%M %d %Y %l.%i%p") FROM myTable;


See also the manual (http://www.mysql.com/documentation/mysql/bychapter/manual_Reference.html#Date_and_time_functions).

BTW: Please don't write these "help, I'm a newbie pleas". That won't urge people to reply to you any faster than usual.

AryaputrA
06-12-2003, 11:29 PM
I'm using phpMyAdmin

I've checked out few tutorials the whole night, and i've solved and got answer to my question, here's how i managed to do it

i added a default value to my date column in my MySql db which is :

d F Y H:i a

then when i'm adding to the database on the query insert line i added :

$news_date = date("d F Y H:i a");

this gave me the result of displaying my date as :

02 January 2003 05:30 am ( this was what i was looking for )

i've tried the now() method but it just displayed 2003 06 15

Thank you very much for replying to my topic, and sorry because i used the Help,I'm a newbie line :)