sseeley Posted January 1, 2009 Share Posted January 1, 2009 Hi I am looking for some help in converting a date to allow me to submit to a MYSQL datebase. I want to convert 'dd/mm/yyyy' to 'yyyymmdd'. For example a user enterd '01/01/2009' and I need to reformat to '20090101' so I can submit to my database. Thanks in advance for any help. Stuart Link to comment https://forums.phpfreaks.com/topic/139131-convert-date/ Share on other sites More sharing options...
DarkSuperHero Posted January 1, 2009 Share Posted January 1, 2009 <?php $date = 'dd/mm/yyyy'; //original string $piece = explode('/',$date); //array created by explode, containign different peices of the date... $date2 = $piece[2].$piece[1].$piece[0]; echo $data2; //returns yyyymmdd print_r($piece); /* array( [0] = 'dd'. [1] = 'mm' [2] = 'yyyy' ) */ this relies on the input date always being input at the same format.... Link to comment https://forums.phpfreaks.com/topic/139131-convert-date/#findComment-727678 Share on other sites More sharing options...
ratcateme Posted January 1, 2009 Share Posted January 1, 2009 this should do it $time = strtotime("01/01/2009"); echo date("ymd",$time); it will also work if the user is missing the leading zeros on the day or month Scott. Link to comment https://forums.phpfreaks.com/topic/139131-convert-date/#findComment-727681 Share on other sites More sharing options...
ratcateme Posted January 1, 2009 Share Posted January 1, 2009 <?php $date = 'dd/mm/yyyy'; $data = explode('/',$date); $date2 = $data[2].$data[1].$data[0]; echo $data2; //returns yyyymmdd this still wouldn't work because of leading zeros but your example can also be write as echo str_replace("/",""$date); Scott. Link to comment https://forums.phpfreaks.com/topic/139131-convert-date/#findComment-727682 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.