数据库
首页 > 数据库> > php – 将碳日期转换为mysql时间戳.

php – 将碳日期转换为mysql时间戳.

作者:互联网

我在mysql数据库中有一个timestamp变量列.试图将碳时间戳转换为我可以在那里输入的内容,但Carbon :: now()只返回一个Carbon对象,当我尝试使用Carbon对象的时间戳字符串时,它不会在mysql中注册.

public function store(CreateArticleRequest $request){
        $input = $request->all(); 
        var_dump($input); // JUST SO YOU CAN SEE
        $input['published_at'] = Carbon::now(); 
        var_dump($input); // JUST SO YOU CAN SEE
        Article::create($input);   
}

我的第一个var转储是这样的:

array (size=4)
  '_token' => string 'Wy67a4hWxrnfiGz61wmXfYCSjAdldv26wOJiLWNc' (length=40)
  'title' => string 'ASDFasdf' (length=8)
  'body' => string 'asdfasdf' (length=8)
  'published_at' => string '2015-08-26' (length=10)  

我的第二个var转储是这样的.

与“published_at”相关的mysql列是时间戳变量.我想如何从碳对象转换它?

提前致谢.

解决方法:

您还可以在模型上设置Mutator.

public function setPublishedAt($value)
{
    $this->attributes['published_at'] = strtotime($value);
}

转换为时间戳

$model -> setPublishedAt('2015-08-26'); // 1440572400

或者您可以使用strtotime将日期转换为时间戳

strtotime — Parse about any English textual datetime description into a Unix timestamp

希望这有帮助.

标签:php,laravel,mysql,timestamp,php-carbon
来源: https://codeday.me/bug/20191001/1840478.html