Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL?

In PHP, you could : Transform the date to a timestamp, using strtotime Format it, using date A bit like this, I'd say : $timestamp = strtotime($date_from_db); echo date('d/m/Y', $timestamp); But this will only work for dates between 1970 and 2038, as timestamps are stored as 32 bits integers, counting from 1970-01-01. In MySQL, I suppose the date_format function would do the trick. For example : mysql> select date_format(curdate(), '%d/%m/%Y'); +------------------------------------+ | date_format(curdate(), '%d/%m/%Y') | +------------------------------------+ | 19/03/2010 | +------------------------------------+ 1 row in set (0.03 sec) And, for the sake of completness, another solution, in PHP, that doesn't suffer from the limitation of 1970-2038 would be to use the DateTime class, and, especially : DateTime::__construct to parse the date returned by the DB DateTime::format to format the date to whatever format you want.

For example, this portion of code : $date = new DateTime('2010-03-19'); echo $date->format('d/m/Y'); would get you this output : 19/03/2010.

– Pascal MARTIN Mar 19 '10 at 20:02 I'm just a bit confused where I insert it! :S Sorry, I'll have have a play and work it out :) – Jess Mar 19 '10 at 20:12 If you choose one of the PHP solutions, you can do that somewhere arround your display, as it's a matter of presentation ;;; if you choose the SQL solution, well, obvisouly, you'll do that in your SQL query (But considering this is presentation, I would prefer having this in the view/template, in PHP) – Pascal MARTIN Mar 19 '10 at 20:14 Ok, what would you say is the best solution to use if I am planning on sorting the table rows by date (afterwards), and also running a query that displays items with dates that have passed a deadline, from what the system date is (if that makes sense)... e.g. System date is set as 20th march 2010, a deadline for activity is set as 21st march...i plan on changing the colour of the table row to red (just an example) to shows its passed date! Will using PHP or MySQL to format the date make any difference to my requirment?

– Jess Mar 19 '10 at 20:25.

Just use the Mysql built in function DATE_FORMAT() SELECT DATE_FORMAT(some_date_field, "Y/m/d").

1 This, except the format is wrong in the example. :) – Mikael S Mar 19 '10 at 20:06.

After getting it from the DB, use $time = strtotime($value) to convert it to a timestamp in PHP. Then use date("d/m/Y", $time) to get it in that format.

You can display you date in any format you want in your pages, in mysql I realy don't know, in php you can use this function: date ( string $format , int $timestamp ). So you can do this: echo date( "d/m/Y", strtotime ( $your_date ) ); You can find full description here: php.net/manual/en/function.date.php.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions