...

View Full Version : Date problem...



stfc_boy
11-14-2007, 05:19 PM
Hello,

I'm looking to post a (job)Date in a MYSQL database in the format of YYYY-MM-DD. However, when the user enters the date in the textbox I want it in the following format D-M-YYYY (IE 1-1-2008)

So here's my code for posting the data for he database:



if(!isset($_GET['note_id']))
{
$result = mysql_query("Insert into notes(jobDate,about) values('$jobDate','$about')");
$note_id=mysql_insert_id();
header("Location: edit_list.php");
}
else
{
$result = mysql_query("Update notes set jobDate='$jobDate', about='$about' where note_id=".$_GET['note_id']);
header("Location: edit_list.php");
}


So how do I turn it around so that when I post the date in the database it coverts to a YYYY-MM-DD format, and when I get the data out of the database it is displayed in by textbox as D-M-YYYY for editing purposes.

Any help most welcome ;-)

Thank you

mlse
11-14-2007, 05:45 PM
Hi stfc,

Assuming your display date is in the precise format "D-M-YYYY" (and assuming that by "D" and "M" you are implying variable width format), you can use the following conversions:

D-M-YYYY to YYYY-MM-DD:


$tokens = explode("-", $dmyyyy);
$yyyymmdd = $tokens[2]."-".sprintf($tokens[1], "%02d")."-".sprintf($tokens[0], "%02d");


YYYY-MM-DD to D-M-YYYY:


$tokens = explode("-", $yyyymmdd);
$dmyyyy = intval($tokens[2])."-".intval($tokens[1])."-".$tokens[0];

aedrin
11-14-2007, 06:04 PM
Or you could do without the complicated conversion, and just use STR_TO_DATE (http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date).

mlse
11-15-2007, 10:29 AM
Yes, that's clearly more sensible! I wasn't aware of the STR_TO_DATE function ...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum