2014-07-19 60 views
0

我得到的数据从数据库到HTML页面日期转换到PHP

 <?php $subscriber = json_decode($this->subscriber); ?> 

我显示的所有元素作为

 <?php echo $subscriber->date; ?> 

哪里像现在我要显示的日期。当我直接显示日期时我得到的格式为2014-07-15 17:02:50

但我想显示格式15-07-2014。使用哪些函数以及如何在PHP中编写代码?

回答

1

使用PHP的DateTime类做的面向对象的方法:

$dt = new DateTime($subscriber->date); 
echo $dt->format('d-m-Y'); 

或者你也可以从字符串首先转换日期使用strtotime() Unix时间戳然后用date()函数格式化为Vijayaragavendran,他在回答中提到:

echo date('d-m-Y',strtotime($subscriber->date)); 
+0

Lol看起来像你在你的更新中做了类似的错字...'y' :) –

+0

哈哈是的,修复它。 – TiMESPLiNTER

3

使用日期()函数

date('d-m-y',strtotime($subscriber->date))