Converting a dat input from HTML form to SQL datetime format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sunegtheoverlord
Forum Commoner
Posts: 28
Joined: Thu Feb 18, 2010 5:02 am

Converting a dat input from HTML form to SQL datetime format

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

Easy in PHP: date('format', strtotime('dd/mm/yyyy'))
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
sunegtheoverlord
Forum Commoner
Posts: 28
Joined: Thu Feb 18, 2010 5:02 am

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

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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')));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply