A JM Posted September 24, 2009 Share Posted September 24, 2009 When reading the date from MySQL and displaying them in the field on my form the format is yyyy-mm-dd. How can I change that to display mm/dd/yyyy? I currently display the records date field like so: value="<?php echo htmlentities($row_rstdetail['filed_date'], ENT_COMPAT, 'utf-8'); ?>" Thanks. A JM, Link to comment https://forums.phpfreaks.com/topic/175346-solved-reading-dates-from-mysql-format-problem/ Share on other sites More sharing options...
smerny Posted September 24, 2009 Share Posted September 24, 2009 do it when you are pulling it from the database so instead of just GET date use GET DATE_FORMAT(date,'%m/%d/%Y') AS date Link to comment https://forums.phpfreaks.com/topic/175346-solved-reading-dates-from-mysql-format-problem/#findComment-924092 Share on other sites More sharing options...
A JM Posted September 25, 2009 Author Share Posted September 25, 2009 Since I'm using a SELECT * that would mean I would have to resort to pulling each field in the SQL statement...hmmm.. Could I write a custom function? I'm not positive about how a PHP is processed so I'm sure if that would work or not..? Thanks for the suggestion. Link to comment https://forums.phpfreaks.com/topic/175346-solved-reading-dates-from-mysql-format-problem/#findComment-924503 Share on other sites More sharing options...
A JM Posted September 25, 2009 Author Share Posted September 25, 2009 Maybe something like this? <?PHP function ($theValue){ list($date, $month, $year) = explode("-", $theValue); if (checkdate($month, $date, $year)) { $pol_expdate = $month . '/' . $date . '/' . $year; $theValue = $pol_expdate; return $theValue; }else{ return $theValue; } ?> Link to comment https://forums.phpfreaks.com/topic/175346-solved-reading-dates-from-mysql-format-problem/#findComment-924509 Share on other sites More sharing options...
A JM Posted September 25, 2009 Author Share Posted September 25, 2009 Ultimately I fixed it with the following function.. let me know if anyone sees a problem with it. <?php function ConvertDate($theValue) { if ($theValue != NULL) { list($year, $month, $date) = explode("-", $theValue); $theValue = $month . '/' . $date . '/' . $year; return $theValue; }else{ return NULL; } } ?> Link to comment https://forums.phpfreaks.com/topic/175346-solved-reading-dates-from-mysql-format-problem/#findComment-924553 Share on other sites More sharing options...
5kyy8lu3 Posted September 25, 2009 Share Posted September 25, 2009 that works. you can also use php's date() function too if you prefer, it's probably faster and less code. date("Y/m/d", $DateFromTable); //kinda like this Link to comment https://forums.phpfreaks.com/topic/175346-solved-reading-dates-from-mysql-format-problem/#findComment-924648 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.