从:
开始$date = '2012-09-09 03:09:00'
我想做两件事。
"2012-09-09"
。有人能帮我解决这个问题吗?
答案 0 :(得分:38)
使用DateTime:
$date = '2012-09-09 03:09:00';
$createDate = new DateTime($date);
$strip = $createDate->format('Y-m-d');
var_dump($strip); // string(10) "2012-09-09"
$now = new DateTime();
$difference = $now->diff($createDate, true);
var_dump($difference);
/* object(DateInterval)#3 (8) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(7)
["h"]=>
int(13)
["i"]=>
int(4)
["s"]=>
int(38)
["invert"]=>
int(0)
["days"]=>
int(7)
} */
答案 1 :(得分:18)
$date = '2012-09-09 03:09:00';
$dt = new DateTime($date);
echo $dt->format('Y-m-d');
$interval = $dt->diff(new DateTime());
您可以使用适合您的间隔。见http://php.net/manual/en/class.dateinterval.php