Page 1 of 1

Inserting date into MySQL

Posted: Mon Oct 20, 2003 4:15 am
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

Posted: Mon Oct 20, 2003 6:31 am
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

Posted: Mon Oct 20, 2003 6:39 am
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

Posted: Mon Oct 20, 2003 6:51 am
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