TRI0N Posted May 15, 2007 Share Posted May 15, 2007 I'm having a serious brain fart here. Use to know how to do this with explode. MySQL Date Field in format of "Y-m-d" and want to convert that to "m-d-Y" for display in a php page. Something like explode("-",$d_date) or something like that.. Or it had %'s in it to seperate each portion in 3 parts. I'm pulling my hair out trying to remember how it's done. Best regards, TRI0N Quote Link to comment https://forums.phpfreaks.com/topic/51486-date-from-y-m-d-to-m-d-y/ Share on other sites More sharing options...
fenway Posted May 15, 2007 Share Posted May 15, 2007 You need to use the DATE_FORMAT() function. Quote Link to comment https://forums.phpfreaks.com/topic/51486-date-from-y-m-d-to-m-d-y/#findComment-253545 Share on other sites More sharing options...
Dablue Posted May 15, 2007 Share Posted May 15, 2007 DATE_FORMAT() is a darn site easier, but FYI using the CURDATE() function, you could use the following... $sql = "SELECT CURDATE()"; $query = mysql_query($sql, $conn); $row = mysql_fetch_array($query); $date = explode('-', $row[0]); $date_y = $date[0]; $date_m = $date[1]; $date_d = $date[2]; $array = array("$date_m", "$date_d", "$date_y"); $new_date = implode("-", $array); echo $new_date; Quote Link to comment https://forums.phpfreaks.com/topic/51486-date-from-y-m-d-to-m-d-y/#findComment-253553 Share on other sites More sharing options...
TRI0N Posted May 15, 2007 Author Share Posted May 15, 2007 This is a stored date in an existing database. Its in the Y-m-d format. Now since I'm pulling out more then just the date using a query but don't want to do a whole new query just for date. In terms will this work? $d_date = $row[21] ; $date = explode('-', $d_date) ; $date_y = $date[0] ; $date_m = $date[1] ; $date_d = $date[2] ; $disp_date = $date_m."/".$date_d."/".$date_y ; I would think this would work... Quote Link to comment https://forums.phpfreaks.com/topic/51486-date-from-y-m-d-to-m-d-y/#findComment-253574 Share on other sites More sharing options...
Dablue Posted May 15, 2007 Share Posted May 15, 2007 That should do the trick mate, looks good to me Quote Link to comment https://forums.phpfreaks.com/topic/51486-date-from-y-m-d-to-m-d-y/#findComment-253576 Share on other sites More sharing options...
fenway Posted May 15, 2007 Share Posted May 15, 2007 Like I said, the DB can handle this... but implement how ever you want. Quote Link to comment https://forums.phpfreaks.com/topic/51486-date-from-y-m-d-to-m-d-y/#findComment-253653 Share on other sites More sharing options...
irvieto Posted May 16, 2007 Share Posted May 16, 2007 Hi. I guess that would be easy if you use a function to change your date to the new format.. Maybe this helps <?php function change_date_format($date){ list($year,$month,$day) = explode("-",$date); $newdate = $month."-".$day."-".$year; return $newdate; } // So.. if $row[0]="2007-05-15"; echo change_date_format($row[0]); //prints 15-05-2007; ?> Hope this helps.. ---> I.R.V.<---- Quote Link to comment https://forums.phpfreaks.com/topic/51486-date-from-y-m-d-to-m-d-y/#findComment-254141 Share on other sites More sharing options...
fenway Posted May 18, 2007 Share Posted May 18, 2007 Again, DATE_FORMAT() is your friend, but for some reason, people like a PHP solution. Quote Link to comment https://forums.phpfreaks.com/topic/51486-date-from-y-m-d-to-m-d-y/#findComment-256657 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.