petenaylor Posted August 24, 2011 Share Posted August 24, 2011 Hi all I have an SQL database that holds dates in this format: yymmdd example: 110824 I am echoing the value: <?php echo $showdata['date']; ?> How do I reverse the string so it shows 240811 ? Cheers Pete Link to comment https://forums.phpfreaks.com/topic/245635-reverse-date-from-mysql/ Share on other sites More sharing options...
msaz87 Posted August 24, 2011 Share Posted August 24, 2011 $date = date("dmy",strtotime($showdata['date'])); Link to comment https://forums.phpfreaks.com/topic/245635-reverse-date-from-mysql/#findComment-1261600 Share on other sites More sharing options...
petenaylor Posted August 24, 2011 Author Share Posted August 24, 2011 Thanks for your quick reply! That just shows today's date instead of the date from the DB? How should I echo out the $date? Pete Link to comment https://forums.phpfreaks.com/topic/245635-reverse-date-from-mysql/#findComment-1261602 Share on other sites More sharing options...
msaz87 Posted August 24, 2011 Share Posted August 24, 2011 Thanks for your quick reply! That just shows today's date instead of the date from the DB? How should I echo out the $date? Pete It doesn't like the format of the date you're storing... I had to test it to make sure. Your two options are to store the date in a different way (e.g. YYYY-MM-DD) or you can use another solution like just rearranging the numbers in the string. $first = substr($showdata['date'], 4, 2); $second = substr($showdata['date'], 2, 2); $third = substr($showdata['date'], 0, 2); $newDate = $first.$second.$third; Link to comment https://forums.phpfreaks.com/topic/245635-reverse-date-from-mysql/#findComment-1261607 Share on other sites More sharing options...
Pikachu2000 Posted August 25, 2011 Share Posted August 25, 2011 You should really be storing the date in the correct format to begin with, but you can do what you're asking with MySQL's STR_TO_DATE() and DATE_FORMAT() functions as well, and it will probably be more efficient also. SELECT DATE_FORMAT(STR_TO_DATE(`date`, '%y%m%e'), '%e%m%y') AS formatted_date FROM table Link to comment https://forums.phpfreaks.com/topic/245635-reverse-date-from-mysql/#findComment-1261688 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.