shadiadiph Posted January 15, 2009 Share Posted January 15, 2009 Hi guys on my form I have a date field which saves to sql the problem I have is this when the user inputs the date it is done in this format. <tr> <td id="t_date">Date </td> <td><input name="date" type="text" class="textfield" value="<?=$tradedate?>"> enter date as (dd-mm-yyyy) </td> </tr> so the date is being entered for exmple 30-10-2009 when i do the sql insert it won't allow it because the date always says it saved as 0000-00-00? $insertsql ="insert into tbltradedetails (date) values ('$date')"; Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/ Share on other sites More sharing options...
Daniel0 Posted January 15, 2009 Share Posted January 15, 2009 Make sure that $date is actually set. Try to echo it first to check. Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-737901 Share on other sites More sharing options...
shadiadiph Posted January 15, 2009 Author Share Posted January 15, 2009 date is inputted as 15-01-2009 on the form page then for submit page $date = ($_POST["date"]); $insertsql ="insert into tbltradedetails ($date) values ('$date')"; but i keep getting 0000-00-00 Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-737907 Share on other sites More sharing options...
Zhadus Posted January 15, 2009 Share Posted January 15, 2009 MySQL doesn't understand it in that format, just need to reformat before inserting into the DB. $date = "30-10-2009"; $date = date('Y-m-d', strtotime($date)); echo $date; // Output is 2009-10-30 Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-737909 Share on other sites More sharing options...
shadiadiph Posted January 15, 2009 Author Share Posted January 15, 2009 WIERD I JUST TRIED THIS $date = $tradedate; $date = date('Y-m-d', strtotime($date)); echo $date; the output was 2020-07-01 the initial input of $tradedate was 15-01-2009??? Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-737914 Share on other sites More sharing options...
Zhadus Posted January 15, 2009 Share Posted January 15, 2009 That's odd, I actually tested that code. print $tradedate to the screen before doing the strtotime() function and post exactly what it says. Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-737916 Share on other sites More sharing options...
shadiadiph Posted January 15, 2009 Author Share Posted January 15, 2009 very odd i just did print $tradedate and it came up as 15-01-2009 but once it goes through that other code it comes out as 2020-07-01 Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-737923 Share on other sites More sharing options...
Zhadus Posted January 15, 2009 Share Posted January 15, 2009 Can you post the code where $tradedate is coming from? Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-737927 Share on other sites More sharing options...
shadiadiph Posted January 15, 2009 Author Share Posted January 15, 2009 <tr> <td id="t_tradedate">Date </td> <td><input name="tradedate" type="text" class="textfield" value="<?=$tradedate?>"> enter date as (dd-mm-yyyy) </td> </tr> does date('Y-m-d' call todays date? Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-737930 Share on other sites More sharing options...
kenrbnsn Posted January 15, 2009 Share Posted January 15, 2009 The strtotime function does not understand a date in the form dd-mm-yyyy. It does understand mm-dd-yyyy and yyyy-mm-dd. You need to reformat the input: <?php $date = "30-10-2009"; list($mm,$dd,$yyyy) = explode('-',$date); $newdate = $yyyy . '-' . $mm . '-' . $dd; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-737934 Share on other sites More sharing options...
shadiadiph Posted January 15, 2009 Author Share Posted January 15, 2009 mm then i need to use something else as the user has to put in 15-01-2009 format european format Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-737941 Share on other sites More sharing options...
shadiadiph Posted January 16, 2009 Author Share Posted January 16, 2009 there must be an easy way to this preserving the date input format or is sql only friendly for the USA? Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-738060 Share on other sites More sharing options...
shadiadiph Posted January 16, 2009 Author Share Posted January 16, 2009 this seems to work $date = explode("-","$tradedate"); $newdate = strftime("%Y-%m-%d",mktime(0,0,0,$date[1],$date[0],$date[2])); if it helps someone in the future in theory i guess when I call the data from the database i could use the same method to display it back as 15-01-2009 also Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-738083 Share on other sites More sharing options...
Daniel0 Posted January 16, 2009 Share Posted January 16, 2009 there must be an easy way to this preserving the date input format or is sql only friendly for the USA? TIME/DATE/DATETIME are formatted according to ISO 8601. Quote Link to comment https://forums.phpfreaks.com/topic/140990-solved-date-problem/#findComment-738201 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.