Jump to content

Reverse Date from mySQL


petenaylor

Recommended Posts

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;

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.