mysql对字段格式化日期_在mysql中格式化日期字段

bd96500e110b49cbb3cd949968f18be7.png

I have a birthdate field stored in a table in the database.At present it displays the values as 2005-10-12 but i wanted to display as 10-12-2005.I have tried the following query but it doesnt display the field at all in PHP.Any suggestions will be helpful.By the way this query when executed directly on the database it is dispalyed but not when in php.Thanks in advance.

SELECT DATE_FORMAT(birthdate,'%m-%d-%Y'),name FROM persons ORDER BY name DESC

解决方案

It should display properly. If you are trying to get the field in PHP for example, it doesn't appear as birthdate, but rather as it's written: DATE_FORMAT(birthdate,'%m-%d-%Y'). That's probably why it is not in $row['birthdate'] but rather in $row["DATE_FORMAT(birthdate,'%m-%d-%Y')"], if at all.

To get it as birthdate, use an alias to the field with as keyword:

SELECT DATE_FORMAT(birthdate,'%m-%d-%Y') as birthdate, name FROM persons ORDER BY name DESC

Now it fill be found in $row['birthdate'].

A more flexible way is to select the raw date (preferably in UNIX_TIMESTAMP format) and format the date in your programming language. In PHP, you could do something like this:

$query = "SELECT UNIX_TIMESTAMP(birthdate) as birthdate, name FROM persons ORDER BY name DESC";

$resource = mysql_query($query);

while($row = mysql_fetch_assoc($resource)) {

echo date('m-d-Y', $row['birthdate'])." ".$row['name']."
";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值