Solved

insert australian date format into mysql database

Posted on 2007-03-18
10
759 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
Industry Leaders: 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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

756 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