如何在php中更改日期时间格式的日期格式

i have mysql database in that i have one field of datetime datatype where date is stored as

2013-09-07 13:20:35

now i want to display this date and time on webpage something like this

07-09-2013 1:20:35 AM

is there any php function/method to achieve this.

Thanks in advance

try this code

echo date('d-m-Y H:i:s', strtotime($yourVariable));

I'd certainly hope that date values aren't actually stored in a textual format at all, any more than numbers are. It's a bit like asking the database to store numbers as hex instead of decimal.

You should separate the inherent values within the data from the textual format applied to them when you present them. Normally I'd recommend formatting the data at the client side instead (which typically has more user context) but you can specify a format in the SELECT statement as well:

SELECT DATE_FORMAT(StartDate, '%d-%m-%y') FROM SomeTable

Likewise when inserting or updating data, I would try to use parameterized SQL so that you don't specify dates as text at all - normally the client library should be able to pass the data on to the database in a native binary format.

In general, you should avoid string conversions unless they're actually necessary (e.g. for display to the user). They're very often a source of subtle errors, particularly around internationalization (where date formats can differ between cultures). See my blog post on data meaning for more details.