I am excepting date from the user in a format "d/m/Y" (eg. "22/03/2007")
and want to submit in the format "Y/m/d" ("2007/03/22")
because Mysql store date in this format
Please help
Here is a different method that also validates the format (but not the actual values) and is also not limited by the Unix timestamp min/max values -
PHP Code:
if(preg_match('@(\d{1,2})/(\d{1,2})/(\d{4})@',$date,$parts) == 1)
{
// This puts the d (or dd), m (or mm), and year in $parts[1] through $parts[3]
// have a valid date, put into yyyy-mm-dd format
$mysql = sprintf("%d-%02d-%02d",$parts[3],$parts[2],$parts[1]);
echo "Before: $date, after: $mysql<br />";
} else {
echo "The date format: $date was invalid<br />";
}
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Version which adds date validation. No human enters dates expecting out of range rollover/rollback. Such values would indicate a typo was made and should not be processed further. -
PHP Code:
if(preg_match('@(\d{1,2})/(\d{1,2})/(\d{4})@',$date,$parts) == 1)
{
// This puts the d (or dd), m (or mm), and year $parts[1] through $parts[3]
// echo "<pre>",print_r($parts),"</pre>";
// have a valid date, put into yyyy-mm-dd format
$mysql = sprintf("%d-%02d-%02d",$parts[3],$parts[2],$parts[1]);
if(!checkdate($parts[2], $parts[1], $parts[3]))
{
echo "The date: $mysql is invalid<br />";
} else {
echo "Before: $date, after: $mysql<br />";
}
} else {
echo "The date format: $date is invalid<br />";
}
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Version which adds date validation. No human enters dates expecting out of range rollover/rollback. Such values would indicate a typo was made and should not be processed further. -
PHP Code:
if(preg_match('@(\d{1,2})/(\d{1,2})/(\d{4})@',$date,$parts) == 1) { // This puts the d (or dd), m (or mm), and year $parts[1] through $parts[3] // echo "<pre>",print_r($parts),"</pre>"; // have a valid date, put into yyyy-mm-dd format $mysql = sprintf("%d-%02d-%02d",$parts[3],$parts[2],$parts[1]); if(!checkdate($parts[2], $parts[1], $parts[3])) { echo "The date: $mysql is invalid<br />"; } else { echo "Before: $date, after: $mysql<br />"; } } else { echo "The date format: $date is invalid<br />"; }
Yes, that works good.
Here's the same thing in boolean logic.
PHP Code:
$mysql = preg_match('@(\d{1,2})/(\d{1,2})/(\d{4})@',$date,$parts) ? checkdate($parts[2], $parts[1], $parts[3]) ? sprintf("%d/%02d/%02d",$parts[3],$parts[2],$parts[1]) : "The date format: $date is invalid<br />" : ""; if(!is_numeric($mysql[0])){echo $mysql."<br />";} else { // Do something with $mysql. Like store it in a database. echo $mysql; }
Or you could check for the "T" in the error message:
PHP Code:
$mysql = preg_match('@(\d{1,2})/(\d{1,2})/(\d{4})@',$date,$parts) ? checkdate($parts[2], $parts[1], $parts[3]) ? sprintf("%d/%02d/%02d",$parts[3],$parts[2],$parts[1]) : "The date format: $date is invalid<br />" : ""; if($mysql[0] == "T"){echo $mysql."<br />";} else { // Do something with $mysql. Like store it in a database. echo $mysql; }