在Cake中为Cakephp从日期时间中提取日期

时间:2013-06-29 16:05:03

标签: php cakephp datetime cakephp-2.0 cakephp-2.1

我的cakephp中有一个表,其字段名称为 datetime ,数据类型为 datetime 以这种格式存储

 2013-06-18 00:00:00

我需要提取值的日期部分而不是时间..

我像这样提取时间

        $dateTime = $recentCall['Calllog']['dateTime'];
    $time = date("H:i:s",strtotime($datetime)); 

现在我想提取日期部分。我不知道我怎么能这样做..我已经做了一些研究,但没有任何效果对我来说

2 个答案:

答案 0 :(得分:3)

这不是CakePHP的答案,而是PHP的答案。

$dateTime  = $recentCall['Calllog']['dateTime'];
$timestamp = strtotime($dateTime);
$date      = date('Y-m-d' , $timestamp);
$time      = date('H:i:s' , $timestamp);

如果你想更多地思考Cakeish并使用CakePHP包装器,应该是:

$dateTime  = $recentCall['Calllog']['dateTime'];
$timestamp = CakeTime::fromString($dateTime);
$date      = CakeTime::format($dateTime, 'Y-m-d');
$time      = CakeTime::format($dateTime, 'H:i:s');

我希望我的回答很清楚。 参考:http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html

答案 1 :(得分:0)

使用Y-m-d作为date中的第一个参数来提取日期。

$date = date("Y-m-d",strtotime($datetime));