AdRock Posted January 13, 2007 Share Posted January 13, 2007 I have a form where my admin user has to enter a date of an event.He has entered the date in a UK format such as 25/11/07 but i need to convert into a SQL date so i can insert it into my MySQL databaseHow do i convert the date as it reads something really random when i display the date on the page Link to comment https://forums.phpfreaks.com/topic/34021-changing-form-date-to-mysql-date-to-insert-into-database/ Share on other sites More sharing options...
emehrkay Posted January 13, 2007 Share Posted January 13, 2007 list($day, $month, $year) = explode('/', $date);$sqlDate = $year .'-'. $month .'-'. $day .' 00:00:00'; Link to comment https://forums.phpfreaks.com/topic/34021-changing-form-date-to-mysql-date-to-insert-into-database/#findComment-159924 Share on other sites More sharing options...
paul2463 Posted January 13, 2007 Share Posted January 13, 2007 would this not work too?[code]<?php$sqlDate = date("Y-m-d", strtotime(inputDate)); ?>[/code] Link to comment https://forums.phpfreaks.com/topic/34021-changing-form-date-to-mysql-date-to-insert-into-database/#findComment-159940 Share on other sites More sharing options...
ShogunWarrior Posted January 13, 2007 Share Posted January 13, 2007 It should, but I've always been suspicous of how exactly strtotime can know if your date string is "dd/mm/yy" or "mm/dd/yy". In this example "25/11" is obvious but I'm not sure about "11/11" ? Link to comment https://forums.phpfreaks.com/topic/34021-changing-form-date-to-mysql-date-to-insert-into-database/#findComment-159941 Share on other sites More sharing options...
AdRock Posted January 13, 2007 Author Share Posted January 13, 2007 On my form I have a text filed called txtEventHere is the code I use to create the $event variable[code]$event = $_POST['txtEvent'];[/code]and this is the code to coonvert the date to SQl format[code]$EventDate = date('Y/m/d', strtotime($event));[/code]When i echo $EventDate it is screwing the date right up somehting like 12/04/2031 Link to comment https://forums.phpfreaks.com/topic/34021-changing-form-date-to-mysql-date-to-insert-into-database/#findComment-159971 Share on other sites More sharing options...
AdRock Posted January 13, 2007 Author Share Posted January 13, 2007 I have just tried messing around with this to try and convert a date but it is still not working properlyI put in 21/9/2006 and it returns 2007/09/09[code]<?php $event = $_POST['txtEvent']; if (!isset($_POST['txtEvent'])) {?> <h2>Add a forthcoming event</h2><form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> <p>Please enter a date for the event. <input id="calendar" class="form" type="text" title="Please enter the event date" size="30" name="txtEvent"></p> <p> <input type="Submit" value="Submit"><input type="reset" value="Reset"></form><?php}else{$event = str_replace("-","/",$event);$date = date('Y/m/d', strtotime($event)); echo $date; }?>[/code] Link to comment https://forums.phpfreaks.com/topic/34021-changing-form-date-to-mysql-date-to-insert-into-database/#findComment-160098 Share on other sites More sharing options...
paul2463 Posted January 15, 2007 Share Posted January 15, 2007 try breaking the strtotime out[code]<?php}else{$event = str_replace("-","/",$event);$changedEvent = strtotime($event); $date = date('Y/m/d', $changedEvent ); echo $date; }[/code]?> Link to comment https://forums.phpfreaks.com/topic/34021-changing-form-date-to-mysql-date-to-insert-into-database/#findComment-161031 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.