如何将函数计算的结果分配给PHP中的变量

时间:2014-05-27 17:53:58

标签: php laravel

我使用laravel's eloquent从数据库中收集数据。我需要将DATETIME值转换为时间戳。我成功地做到了,但无法将其分配回结果。它让我头疼。代码段如下所示。

public function getArticle() {
    $article = new Article;
    $data =  $article::find(Input::get("article_id"));
    $data->created_at = $this->getTimestamp($data->created_at);
    return $data;

}

将日期时间转换为时间戳的功能如下:

public function getTimestamp($datetime) {
    $time = strtotime($datetime);
    return $time;
}

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

Laravel Eloquent使用Carbon。你不需要转换任何东西 - 它可以在结果上自动完成:

printf("Right now is %s", $data->created_at->toDateTimeString());

还有很多其他方法可以操纵Carbon选项。 You can see a full list here at the Carbon Github page

相关问题