我正试图转换这个时间戳格式,我从Oracle sql查询:
19-SEP-11 02.34.51.558459 PM
我需要把它转换成这样的格式:dd-mm-YYYY。
$install_date=strtotime($install_date);
$install_date=date("d/m/Y",strtotime($install_date));
但是我得到奇怪的结果…
任何想法?
*** forgot to mention, the field type is TIMESTAMP and not DATETIME
当处理strtotime()
不能处理的格式时(参见支持的日期和时间格式),或者即使你是,那么你可以使用DateTime::createFromFormat()
(或它的过程双胞胎,date_create_from_format()
) (docs)从任何格式创建DateTime
对象。
$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');
如果是datetime字段,可以使用
TO_CHAR(fieldName, 'DD-MM-YYY')
在你的选择,然后它将被格式化,因为它从数据库中出来
改为:
$install_date=strtotime($install_date);
$install_date=date("d/m/Y",$install_date);
您已经将$install_date
转换为内部时间,然后您在第二行中再次执行此操作。
当然,如果你愿意,你可以把它全部剪成一行:
$install_date=date("d/m/Y",strtotime($install_date));