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 Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/ 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 Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188734 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)); } Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188735 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. Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188787 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. Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188792 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. Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188800 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. Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188805 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 Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188811 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. Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188816 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])); Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188822 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 Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188823 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. Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188825 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 Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188831 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])); Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188834 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 Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188836 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; ?> Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188843 Share on other sites More sharing options...
grimmier Posted February 19, 2007 Author Share Posted February 19, 2007 That works beautifuly. thank you. Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188847 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])); } Link to comment https://forums.phpfreaks.com/topic/39191-solved-need-help-with-date-formating/#findComment-188862 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.