2

how would you convert a date stored as

2011-01-18 11:51:41

into

18-01-2011 11:51:41

using PHP?

many thanks in advance!

1
  • 1
    A date is not stored as anything but just a date. How it looks depends on display formatting. Internally a data is just a number (unless you store it as a string/varchar, in which case it it not a date). Commented Jul 26, 2011 at 20:28

4 Answers 4

5
date('d-m-Y H:i:s', strtotime('2011-01-18 11:51:41')); 
Sign up to request clarification or add additional context in comments.

1 Comment

The problem with this is that on 32-bit servers dates after 2038 will become January 1st 1970.
4

More reliable than using strtotime(), assuming you're on PHP 5.3+

$oldtime = date_parse_from_format('Y-m-d h:i:s', '2011-01-18 11:51:41'); $newtime = date('d-m-Y h:i:s', $time); 

However, the date format you're converting FROM suggests it's coming from a MySQL datetime field, in which case you could also do:

SELECT DATE_FORMAT(yourfield, '%d-%m-%Y %H:%i:%s') 

and save yourself a full roundtrip in PHP.

Comments

3

Convert the old date to UNIX time with strtotime(), then output it in the new format with date()

$olddate = "2011-01-18 11:51:41"; $newdate = date('d-m-Y H:i:s', strtotime($olddate)); echo $newdate; // 18-01-2011 11:51:41 

Comments

2
$your_date = "2011-01-18 11:51:41"; echo date('d-m-Y H:i:s', strtotime($your_date)); 

demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.