简体   繁体   English

php:将日期转换为dd-mm-YYYY格式

[英]php: date conversion to dd-mm-YYYY format

I'm trying to convert this timestamp format which I got from an Oracle sql query: 我正在尝试转换从Oracle sql查询获得的这种时间戳格式:

19-SEP-11 02.34.51.558459 PM

I need to convert it to this format: dd-mm-YYYY. 我需要将其转换为以下格式: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 ). 当与格式处理strtotime()不能处理(见支持的日期和时间格式 ),或者即使你是,那么你可以创建一个DateTime从任何格式使用对象DateTime::createFromFormat()或它的程序双胞胎, 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 如果它是Oracle中的日期时间字段,则可以使用

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. 您已经将$install_date转换$install_date内部时间,然后在第二行中再次进行了转换。

Of course, you can cut it all down to one line if you like: 当然,如果您愿意,可以将其全部缩减为一行:

$install_date=date("d/m/Y",strtotime($install_date));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM