I have created a web page that requires the user to enter a date but the ISO date format (yyyy-mm-dd) is inconvenient for the users. I would like to use the date format of mm-dd-yyyy. How can I do this conversion from php so that the date is still ISO formatted in MySQL? This has to be FAQ somewhere but I haven't found it yet... I am currently using the 'date' type for ccd in the MySQL table.
I also want to detect whether my user has put in the date using periods, dashes or slashes as the delimiters.
I'm not very good at regex and the test code I have come up with so far is something like what follows (although it doesn't work). Can someone tell me what I am doing wrong or suggest a better way to do it?
<?php
$ccd = "04/25/2001";
// fix the date so that you can enter a non-iso date
// this checks to make sure that there is at least one number,
// then a .- or /
// then at least another number
// then a .- or /
// and then 200 followed by a number from 0-9
// if (ereg("[0-9][0-9]?[/.-][0-9][0-9]?[/.-]200[0-9]$", $ccd)) {
//reformat the date for saving into the database
echo "The starting ccd is = " . $ccd . "<p><p>";
if ( ereg("[01]?[0-9][-./][01]?[0-9][-./]200[0-9]$", $ccd ) ) {
echo "This ccd made it through the first if statement " . $ccd . "<p><p>";
switch ( $ccd ) {
case (ereg("[01]?[0-9][/][01]?[0-9][/]200[0-9]$", $ccd) ):
$array_date = explode("/", $ccd);
echo $array_date . "\n";
echo "Everything is working properly in the / statement.<p>";
break;
case (ereg("[01]?[0-9][.][01]?[0-9][.]200[0-9]$", $ccd) ):
$array_date = explode(".", $ccd);
echo $array_date . "\n";
echo "Everything is working properly in the . statement.<p>";
break;
case (ereg("[01]?[0-9][-][01]?[0-9][-]200[0-9]$", $ccd) ):
$array_date = explode("-", $ccd);
echo $array_date . "\n";
echo "Everything is working properly in the - statement.<p>";
break;
default :
// you didn't enter the date in the correct format
echo "You didn't enter the date in the correct format. You entered: |" . $ccd . "|\n<p><p>";
} // end of switch
} // end of ereg
$new_ccd_date = $array_date[2] . "/" . $array_date[0] . "/" . $array_date[1];
echo "The new ccd date is " . $new_ccd_date;
?>
Thanks to all who can help...
--->Rob