在 PHP 中,您可以:
- 将日期转换为时间戳,使用strtotime http://fr.php.net/strtotime
- 格式化它,使用date https://www.php.net/manual/en/function.date.php
有点像这样,我会说:
$timestamp = strtotime($date_from_db);
echo date('d/m/Y', $timestamp);
但这仅适用于 1970 年至 2038 年之间的日期,因为时间戳 http://en.wikipedia.org/wiki/Unix_time存储为 32 位整数,从 1970-01-01 开始计数。
In MySQL, I suppose the [`date_format`][4] function would do the trick.
For example :
mysql> select date_format(curdate(), '%d/%m/%Y');
+------------------------------------+
| date_format(curdate(), '%d/%m/%Y') |
+------------------------------------+
| 19/03/2010 |
+------------------------------------+
1 row in set (0.03 sec)
And, for the sake of completness, another solution, in PHP, that doesn't suffer from the limitation of 1970-2038 would be to use the `DateTime` class, and, especially :
-
DateTime::__construct http://fr.php.net/manual/en/datetime.construct.php解析数据库返回的日期
-
DateTime::format http://fr.php.net/manual/en/datetime.format.php将日期格式化为您想要的任何格式。
例如,这部分代码:
$date = new DateTime('2010-03-19');
echo $date->format('d/m/Y');
会给你这个输出:
19/03/2010