Solved

insert australian date format into mysql database

Posted on 2007-03-18
10
716 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
  • 5
  • 5
10 Comments
 
LVL 142

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 142

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
 
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 142

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 142

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 142

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now