php: date conversion to dd-mm-YYYY format
I'开发者_JAVA技巧m trying to convert this timestamp format which I got from an Oracle sql query:
19-SEP-11 02.34.51.558459 PM
I need to convert it to this format: dd-mm-YYYY.
$install_date=strtotime($install_date);
$install_date=date("d/m/Y",strtotime($install_date));
but I'm getting weird results...
Any ideas?
*** forgot to mention, the field type is TIMESTAMP and not DATETIME
When dealing with formats that strtotime()
cannot handle (see supported date and time formats), or even if you are, then you can create a DateTime
object from any format using DateTime::createFromFormat()
(or it's procedural twin, date_create_from_format()
) (docs).
$install_date = '19-SEP-11 02.34.51.558459 PM';
$datetime = DateTime::createFromFormat('j-M-y h.i.s.u A', $install_date);
$datetime_dmy = $datetime->format('d/m/Y');
If it is a datetime field in Oracle, you could use
TO_CHAR(fieldName, 'DD-MM-YYY')
In your select, then it would be formatted as it comes out of the database.
Do this instead:
$install_date=strtotime($install_date);
$install_date=date("d/m/Y",$install_date);
You had already converted $install_date
to internal time, then you were doing it again in the second line.
Of course, you can cut it all down to one line if you like:
$install_date=date("d/m/Y",strtotime($install_date));
精彩评论