Inserting date into MySQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Inserting date into MySQL

Post by JayBird »

Okay, i have a form where user enter the date in this format DD-MM-YYYY.

This needs to get inserted into a date field in my database. But the date field is formatted YYYY-MM-DD.

Is there something a can do in the query to format the date correctly, or will i have to format it using PHP first?

Thanks

Mark
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Maybe explode() might help. Here's how I'd do it:

Code: Select all

<?php
$date = "20-10-2003"; // of $_POST['date'] or something

$arr = explode("-", $date);
$new = <<< DATE
{$arr[2]}-{$arr[1]}-{$arr[0]}
DATE;

echo $new;
?>
Hope that helps Bech,

-Nay
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

thats the way i have done it.

But is that the best way? I know you can use DATE_FORMAT in SQL when retriving dates, but not sure if you can when inserting dates.

Mark
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

I'm still having a read through but maybe this might do:

http://www.mysql.com/doc/en/Date_and_ti ... tions.html

-Nay
Post Reply