Page 1 of 1

Converting a dat input from HTML form to SQL datetime format

Posted: Mon Feb 22, 2010 9:37 am
by sunegtheoverlord
Hello,

I'm new to SQL and php in general but have come across a problem which i can see alot of people are having however, i can't seem to work out a way around my specific problem from online resources.

I have a simple HTML form which asks for a date from the user. the date is entered in the UK date format (dd/mm/yyyy). SQL Server obviously complains about this as it wants it in the standard datetime format.

How do i convert the user input date to the datetime format? Do i do this in the HTML? the PHP? or in the T-SQL?!

HELP.

S

Re: Converting a dat input from HTML form to SQL datetime format

Posted: Mon Feb 22, 2010 9:49 am
by AbraCadaver
Easy in PHP: date('format', strtotime('dd/mm/yyyy'))

Re: Converting a dat input from HTML form to SQL datetime format

Posted: Tue Feb 23, 2010 3:40 am
by sunegtheoverlord
Hello again,

excuse me if i'm being stupid ( and i probably am) but i can't get that to work.

the code below:-

<?php


echo date("ymd", strtotime('25/02/2000'));


?>

Gives the output 700101 ????

I need it to give 20000225.

I've read the strtotime function requires a US english date input (i.e. mm/dd/yyyy) and i want to input a UK english (dd/mm/yyyy).

What am i doing wrong?

Re: Converting a dat input from HTML form to SQL datetime format

Posted: Tue Feb 23, 2010 9:04 am
by AbraCadaver
strtotime() makes assumptions on the order of the parts depending upon whether you use a / or - so one of the following will work:

Code: Select all

echo date("Ymd", strtotime('25-02-2000'));
// or
echo date("Ymd", strtotime(str_replace('/', '-', '25/02/2000')));