我将用户注册日期和时间存储为 MySQL 中的日期时间。现在要对该日期进行一些处理,我需要分别获取年、月和日才能使用。
例如:
2015-07-30 19:20:34
现在我想要 2015,如何在 PHP 中实现?
最佳答案
date('Y', strtotime(<date string from mysql>));
strtotime
函数(http://php.net/manual/en/function.strtotime.php)将您的日期字符串解析为 Unix 时间戳,然后 date
函数(http://php.net/manual/en/function.date.php)以定义的格式输出它.
所以你可以这样做:
$date = '2015-07-30 19:20:34';
$year = date('Y', strtotime($date));
关于php - 在 PHP 中与 MySQL 日期时间分开获取年/月/日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31727620/