frobak Posted February 7, 2012 Share Posted February 7, 2012 Hi I need to change a uk date dd/mm/yyyy to mysql format yyyy/mm/dd I have a user form where they enter the date in dd/mm/yyyy string format, i want to take that string, and convert it to mysql format yyyy/mm/dd Possible? Ive searched for ages and havent found a solution that works. Is there an easy solution to this? surely there must be Cheers Quote Link to comment https://forums.phpfreaks.com/topic/256632-convert-ddmmyyyy-to-yyyymmdd-for-mysql/ Share on other sites More sharing options...
spiderwell Posted February 7, 2012 Share Posted February 7, 2012 there is an easy solution use explode to turn the elements into an array then you can re arrange them the right order and turn back into a string/date $arrDate = explode("/","01/12/2012"); echo $arrDate[2] . "/" $arrDate[1] . "/" . $arrDate[0] Quote Link to comment https://forums.phpfreaks.com/topic/256632-convert-ddmmyyyy-to-yyyymmdd-for-mysql/#findComment-1315594 Share on other sites More sharing options...
sasa Posted February 7, 2012 Share Posted February 7, 2012 if you need this for MySQL jou can use str_to_date() and date_format() functions in query Quote Link to comment https://forums.phpfreaks.com/topic/256632-convert-ddmmyyyy-to-yyyymmdd-for-mysql/#findComment-1315599 Share on other sites More sharing options...
frobak Posted February 7, 2012 Author Share Posted February 7, 2012 thanks for your reply. So ive tried using the code you supplied, but im getting errors. Heres the code $start_date_pre = $_POST['start_date']; $arrDate = explode("/","$start_date_pre"); $start_date = "$arrDate[2] . / $arrDate[1] . / . $arrDate[0]"; $sql = "INSERT INTO vacancies SET vacancy_id= '$_POST[vacancy_id]', job_title= '$_POST[job_title]', salary= '$_POST[salary]', job_salary_range= '$_POST[job_salary_range]', job_location= '$_POST[job_location]', job_terms= '$_POST[job_terms]', start_date= $start_date, end_date= '$_POST[end_date]', job_desc= '$_POST[job_desc]'"; Can you see where ive gone wrong? Quote Link to comment https://forums.phpfreaks.com/topic/256632-convert-ddmmyyyy-to-yyyymmdd-for-mysql/#findComment-1315601 Share on other sites More sharing options...
spiderwell Posted February 7, 2012 Share Posted February 7, 2012 yes i can, look carefully again and check your " and ' are in the right place. Quote Link to comment https://forums.phpfreaks.com/topic/256632-convert-ddmmyyyy-to-yyyymmdd-for-mysql/#findComment-1315603 Share on other sites More sharing options...
sasa Posted February 7, 2012 Share Posted February 7, 2012 change line start_date= $start_date, to start_date= str_to_date('$start_date_pre', '%d/%m/%Y'), Quote Link to comment https://forums.phpfreaks.com/topic/256632-convert-ddmmyyyy-to-yyyymmdd-for-mysql/#findComment-1315610 Share on other sites More sharing options...
frobak Posted February 7, 2012 Author Share Posted February 7, 2012 heres the code that worked for me. $start_date_pre = $_POST['start_date']; $arrDate = explode("/","$start_date_pre"); $start_date = $arrDate[2] . "/" . $arrDate[1] . "/" . $arrDate[0]; $sql = "INSERT INTO vacancies SET vacancy_id= '$_POST[vacancy_id]', job_title= '$_POST[job_title]', salary= '$_POST[salary]', job_salary_range= '$_POST[job_salary_range]', job_location= '$_POST[job_location]', job_terms= '$_POST[job_terms]', start_date= '$start_date', end_date= '$_POST[end_date]', job_desc= '$_POST[job_desc]'"; Cheers for your help Quote Link to comment https://forums.phpfreaks.com/topic/256632-convert-ddmmyyyy-to-yyyymmdd-for-mysql/#findComment-1315611 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.