Solved

insert australian date format into mysql database

Posted on 2007-03-18
10
774 Views
Last Modified: 2013-12-13
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
0
Comment
Question by:paulp75
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18746355
>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.
0
 
LVL 6

Author Comment

by:paulp75
ID: 18746475
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?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18746551
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.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 6

Author Comment

by:paulp75
ID: 18746751
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.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18746773
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));

0
 
LVL 6

Author Comment

by:paulp75
ID: 18746839
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.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18746848
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...
0
 
LVL 6

Author Comment

by:paulp75
ID: 18746879
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
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18746900

then, the second version (with no spaces before the (  ):

list( $day , $month , $year) = split( $_POST['news_date'] ,  '-' );
$news_date = date( 'Y-m-d' , mktime( 0, 0, 0, $month , $day  $year));
0
 
LVL 6

Author Comment

by:paulp75
ID: 18747337
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question