Log in

View Full Version : Date fields added in mysql



RazorMaster
05-08-2008, 10:03 PM
Hi,

I'm trying to add a field "date" in mysql db.
To this field, will be added a date which becomes from a form by selecting a javascript calendar and inputing it to a textbox.

I've got 2 questions:

1- What format would i get in mysql for date format "D,F j,Y", i.e:

Wednesday, May 28, 2008

2- I have this code to get the value from the form and then other code to insert it into the DB.


$post_date = date("D,F j,Y", $info['post_date']);

But, for some reason, I can't add this value to the DB in mysql.
I've got this fieldset in mysql:

project_done - text(30) NOT NULL

and have this error after I select a day in calendar:

Warning: date() expects parameter 2 to be long, string given in /.../post.php on line 114.
In line 114 here's the code:


$post_date = date("D,F j,Y", $info['post_date']);

Can you help me setting up this date fields???

Thanks

Nile
05-08-2008, 10:16 PM
If you make the project_done a DATE_TIME, and then use the NOW() function(mysql).

RazorMaster
05-08-2008, 10:34 PM
Well, thanks for your idea, but this post_date it's not the now() date, it supose to be a future date, which users may select from javascript calendar.

I want to sabe that date in my db so I can output it on other form which can be seen by other users.

Thanks anyway.

phpsales
05-08-2008, 11:45 PM
The PHP date() module expects the second paramiter to be a Unix time stamp. The PHP error message posted was caused by passing a string as the second parameter.

E.g.
//date(string format, [int timestamp]); // www.php.net/date
echo date("D, F j Y", time()); //Thu, May 8 2008


Better Method
1. Convert MySQL text column to date using SQL:

ALTER TABLE `tablename` CHANGE `project_done` `project_done` DATE NULL;
-- reference: http://dev.mysql.com/doc/refman/5.0/en/alter-table.html
-- reference: http://dev.mysql.com/doc/refman/5.0/en/datetime.html

2. Adjust JavaScript calender settings to use YYYY-MM-DD date formate (this is possible on most dynamic drive calendar scripts)
3. Adjust your insert/update PHP SQL:

//PHP Insert Example
$sql = sprintf("INSERT INTO `tablename` (`project_done`) VALUES ('%s')", $_POST['post_date']);

//PHP Update
$sql = sprintf("UPDATE `tablename` set `project_done` = '%s' WHERE foo = bar", $_POST['post_date']);


Now you can both work with the date in JavaScript and MySQL. MySQL has many very usefull date/time functions that make working with dates easy and almost fun.

MySQL Date/Time Functions: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html