grimmier Posted February 19, 2007 Share Posted February 19, 2007 I have a web form set up to allow entries into a MySQL database, my problem is that i need to be able to convert the date from mm-dd-yyyy format to yyyy-mm-dd format before it gets entered into the database, or it won't go. I have tried to manipulate the string before entering it, but i may have made some errors. any help is greatly appreciated. PS. the user enters the date so i need a way to fail-safe against m-d-yyyy formating as well, due to human nature to omitt the leading Zero's Quote Link to comment Share on other sites More sharing options...
AV1611 Posted February 19, 2007 Share Posted February 19, 2007 is the user using an arbitrary date, or can you just capture the current date at input time? You can use either pulldowns or a js applet to make them pick a real date instead of typing in a date... much nicer IMHO Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 19, 2007 Share Posted February 19, 2007 I believe this should work if (preg_match("@[\d]{2}\-[\d]{2}\-[\d]{4}@", $dateString)) { $newDate=date("Y-m-d", strtotime($dateString)); } Quote Link to comment Share on other sites More sharing options...
grimmier Posted February 19, 2007 Author Share Posted February 19, 2007 is the user using an arbitrary date, or can you just capture the current date at input time? You can use either pulldowns or a js applet to make them pick a real date instead of typing in a date... much nicer IMHO I actually need 2 dates one for date received, and one for date cleared. the date cleared is auto populated with the current date, which is nice. Its the date received that i worry about. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 19, 2007 Share Posted February 19, 2007 It will help if you post the code you have and the errors you got. Quote Link to comment Share on other sites More sharing options...
grimmier Posted February 19, 2007 Author Share Posted February 19, 2007 here is the post code i currently have. it works but only if the date is in right format, otherwise the date fields are filled with 0's. <?php $con = mysql_connect("localhost","root","somepassword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("2007_Inventory", $con); $sql="INSERT INTO Inventory (`Maven ID`, `Owner ID`, `Date Received`, `Date Cleared`, `Status`, `Equipment Type`, `Make`, `Model`, `Serial Number`, `Processor`, `Speed`, `HD`, `RAM`, `Optical Drive`, `Tag #`, `Comments`, `Tested By`) VALUES ('$_POST[mavenID]','$_POST[ownerID]','$_POST[date_rec]','$_POST[date_clear]','$_POST[status]','$_POST[type]','$_POST[make]','$_POST[model]','$_POST[serial]','$_POST[processor]','$_POST[speed]','$_POST[hd]','$_POST[ram]','$_POST[cd]','$_POST[tag]','$_POST[comments]','$_POST[tester]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> if would be nice if i could change the format on behind the scenes before posting the data, and account for various formats. or i might just do as was already recommended and just use a date picker, although this will slow down entry some. PS. this is all set up on an intranet. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 19, 2007 Share Posted February 19, 2007 $time = $_POST['date_clear']; $time = strtotime($time); $time = date("Y-m-d", $time); Then insert $time in your query instead of the POST var. Quote Link to comment Share on other sites More sharing options...
grimmier Posted February 19, 2007 Author Share Posted February 19, 2007 $time = $_POST['date_clear']; $time = strtotime($time); $time = date("Y-m-d", $time); Then insert $time in your query instead of the POST var. I tried this, with the date entered being 01-05-2007 and the output was 1969-12-31 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 19, 2007 Share Posted February 19, 2007 That means nothing was entered for $_POST['date_clear'], because that's the time for 0. Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 19, 2007 Share Posted February 19, 2007 your other option would be mktime: $inputTime=explode($_POST['date_clear'], "-"); $time=date("Y-m-d", mktime($inputTime[2], $inputTime[0], $inputTime[1])); Quote Link to comment Share on other sites More sharing options...
grimmier Posted February 19, 2007 Author Share Posted February 19, 2007 if i enter a shortened date like 1-5-07 it will output 2001-05-07 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 19, 2007 Share Posted February 19, 2007 I think Tom's method might be more reliable. Quote Link to comment Share on other sites More sharing options...
grimmier Posted February 19, 2007 Author Share Posted February 19, 2007 your other option would be mktime: $inputTime=explode($_POST['date_clear'], "-"); $time=date("Y-m-d", mktime($inputTime[2], $inputTime[0], $inputTime[1])); this will always output the current date. i need to be able to enter a date, thats not the current one Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 19, 2007 Share Posted February 19, 2007 Thats cause I made a boo boo... $time=date("Y-m-d", mktime(0,0,0,$inputTime[0],$inputTime[1],$inputTime[2])); Quote Link to comment Share on other sites More sharing options...
grimmier Posted February 19, 2007 Author Share Posted February 19, 2007 1999-11-30 is new output. for all variations on 1-5-07 Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 19, 2007 Share Posted February 19, 2007 I just tested this code, and it works: Can't believe I posted explode with the delimiter as the second argument though... I must be losing my mind. <?php $string="1-5-07"; $inputTime=explode("-", $string); $time=date("Y-m-d", mktime(0,0,0,$inputTime[0],$inputTime[1],$inputTime[2])); echo $time; ?> Quote Link to comment Share on other sites More sharing options...
grimmier Posted February 19, 2007 Author Share Posted February 19, 2007 That works beautifuly. thank you. Quote Link to comment Share on other sites More sharing options...
grimmier Posted February 19, 2007 Author Share Posted February 19, 2007 I took it a step further, incase anyone else has this problem. I put in an if statement to check for users using a slash instead of a dash. $string=$_POST['date_rec']; $dash = '-'; $pos = strripos($string, $dash); if ($pos === false) { $inputTime=explode("/", $string); $time=date("Y-m-d", mktime(0,0,0,$inputTime[0],$inputTime[1],$inputTime[2])); } else { $inputTime=explode("-", $string); $time=date("Y-m-d", mktime(0,0,0,$inputTime[0],$inputTime[1],$inputTime[2])); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.