Link to home
Start Free TrialLog in
Avatar of paulp75
paulp75Flag for Australia

asked on

insert australian date format into mysql database

I am developing a website for an australian business, and I would like to be able to enter the dates of news items into the mysql database in australian format.
I can pull a mysql date out and reformat with no problems, but how do i manage to format the date to mysql format when entering into database.

Australians use the date format Date-Month-Year
I have set up the form to show <?php echo date("d-M-Y") ?> as the output.

when it enters into database it does not work because it is in the wrong format.

this is how it is entered into database

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "WADAInsertForm")) {
  $insertSQL = sprintf("INSERT INTO news (news_title, news_shortdesc, news_content, news_date, news_photoset) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['news_title'], "text"),
                       GetSQLValueString($_POST['news_shortdesc'], "text"),
                       GetSQLValueString($_POST['news_content'], "text"),
                       GetSQLValueString($_POST['news_date'], "date"),
                       GetSQLValueString($_POST['news_photoset'], "int"));


what would i do to insert the date into the correct format
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

>when it enters into database it does not work because it is in the wrong format.
yes. the only format that is valid for date data type is YYYY-MM-DD.
Avatar of paulp75

ASKER

yes but what i'd like to do is format the date into the correct format.
the form needs to have an australian date format as the input standard, as that is what the client reads and understands. no one in australia reads the date as yyyy-mm-dd. its the complete opposite to what we use.
is there a way to format the date when the form is submitted before it is entered into the database?
I assume that you are using php to actualy save the data into your table, so there is the place to reformat the australian format into the mysql required format.
on the other hand, in the form where you read/display the data from the table, you can format the mysql format into the australian format.
Avatar of paulp75

ASKER

thanks for the responses angellll.
thats what I'd like to know how to do. after the person presses submit, i'd like it to have the format changed to mysql format.
the posted data will contain the value in the format: d-M-Y

so, just reverse the order of the array:

list ( $day , $month , $year) = split ( $_POST['news_date'] ,  '-' );
$news_date = mktime ( 0, 0, 0, $month , $day  $year);

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "WADAInsertForm")) {
  $insertSQL = sprintf("INSERT INTO news (news_title, news_shortdesc, news_content, news_date, news_photoset) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['news_title'], "text"),
                       GetSQLValueString($_POST['news_shortdesc'], "text"),
                       GetSQLValueString($_POST['news_content'], "text"),
                       GetSQLValueString($news_date, "date"),
                       GetSQLValueString($_POST['news_photoset'], "int"));


however, not sure what the function GetSQLValueString does with the date value, but I guess it's correct... if not, change the line to build the $news_date to this:
$news_date = date ( 'Y-m-d' , mktime ( 0, 0, 0, $month , $day  $year));

Avatar of paulp75

ASKER

looks like that type of thing would be the way to go, but its giving me unexpected T_VARIABLE error on the line where we change the format. I tried both versions.
let's remove the spaces before the (:

list( $day , $month , $year) = split( $_POST['news_date'] ,  '-' );
$news_date = mktime( 0, 0, 0, $month , $day  $year);

the error is a php parse error...
Avatar of paulp75

ASKER

ok feel like we're getting somewhere. i might have to take a look later. the date output as
943880400943880400
and therefore when it went into the database it still showed up as 0000-00-00
so i'll have to take a look a little later. i feel like we're on the right track though.
thanks so much for your help
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paulp75

ASKER

i ended up getting it right in the end. had to make some changes.
changed it to

list($day, $month, $year) = split( '-' , $_POST['news_date'] );
$news_date = "$year-$month-$day";

and it worked great.

thanks for your persistance. This will help me with a lot of projects. thanks