hey folks,
i am using jQuery datepicker with format of M-dd,yy. and i was wonder as being a newbie in both jQuery as well as php. how do i store it in database or retrieve it from database in the same format?
| SitePoint Sponsor |
hey folks,
i am using jQuery datepicker with format of M-dd,yy. and i was wonder as being a newbie in both jQuery as well as php. how do i store it in database or retrieve it from database in the same format?

If you use the DATE type to store your dates in your mysql database then they will be stored:
YYYY-MM-DD
2010-09-13
So to convert M-dd, yy to say, store something in the correct Mysql you'd use PHP, something basically along the lines of :
CaveatsPHP Code:$name = "William Hunting" ;
$date = date('Y-m-d', strtotime('Sep 13, 10')) ;
// build an insert query
"insert into yourtable startDay, personName values '$date', '$name'" ;
Untested
Not testing for failure, or illegal dates, which youd need to do.
Note the quoting of the values in my simple example which assumes you are escaping the values yourself for security reasons and not using PDO or prepared statements.
Having said that, tinker with your JQuery date picker and see if you cannot just format the return value to be YYYY-MM-DD and save this extra step - or look for one which offers that option.
... and what Cups said regarding validation and security.PHP Code:// insert the date
$input = "8-22,10";
$replace = array("-", ",");
$input = str_replace($replace, "/", $input);
$sql = "INSERT INTO mydates (thedate) VAUES('$mydate')";
// retrieve the date
$sql = "SELECT DATE_FORMAT('mydate', '%c-%d,%y')";
$result = mysql_query($sql);
Bookmarks