views:

1618

answers:

2

Hi, I'm quite new to php and have been reading Larry Ullman book to develop a basic db site. I have used the YUI Calendar pop up date picker to add a date to a text field called"date". The date format it enters is eg Thursday, 7 May 2009

I have tried many different ways to try and enter the date in to mysql db but it remains at 00 00 00 00 00 00 This is the code related to the date field I have,

 // Check for a Date.
if (eregi ("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", $_POST['date'],)) {
 $p = escape_data($_POST['date'],);
} else {
 $p = FALSE;
 echo '<p><font color="red">Please enter a valid Date!</font></p>';
}

 // Add the URL to the urls table.
 $query = "INSERT INTO urls (url, title, description, date) VALUES ('$u', '$t', '$d', '$p')";  
 $result = @mysql_query ($query); // Run the query.
 $uid = @mysql_insert_id(); // Get the url ID.

 if ($uid > 0) { // New URL has been added.

I think I have provided all pertinent information but again apologies if this isn't helpful and I will do my best to provide yo with any other information you may require. Thanks - Sean

A: 

You probably need to format the date in the way that mysql expects it for that datatype, or else it cannot recognize it. You could use a regex or similar method to extract the component parts and format it according to the format of the MySQL DATETIME type.

EDIT: See http://dev.mysql.com/doc/refman/5.0/en/datetime.html for said format. Also, you might be better off storing the date/time as a unix timestamp, as that usually is easier to maintain.

Annath
Hi Thank you. I have been learning about Unix timestamp, and I am tryig to change the text into a value that the MySQL can understand.YYYY MM DD but I am failing to see it so far.Thank you for your advice I will read the link you have sent.
Ddywalgi
A: 

If the format that your date picker is passing in is "Thursday, 7 May 2009", then the strtotime() and date() functions should work to give you a valid date to pass to MySQL:

$p = date("Y-m-d H:i:s",strtotime('Thursday, 7 May 2009'));

If your database field is a DATE, you probably only want the "Y-m-d" part. If it's a DATETIME, you'll want the "H:i:s" as well.

zombat
Thank you Zombat I will research this, I have been looking at mktime strings. Thanks
Ddywalgi
Hi Zombat, I have realised that the date can be either populated by pop up picker or can be typed in.If I want to define the typed date eg 12/02/2009, would this be correct syntax?$date2 = date2("^([0-9]{2})/([0-9]{2})/([0-9]{4})$",strtotime('12/02/2009'));$date1 = date1("Y-m-d",strtotime('Thursday, 7 May 2009'));$p = date("$date1" ,| "$date2");Thank you for your assistance, it is making a bit more sense now.
Ddywalgi
That looks fairly close to correct I think. As long as you can provide a unix timestamp as the second parameter to date(), you'll be able to create a proper MySQL date. strtotime() works very well for this, as well as mktime().
zombat