将碳日期转换为mysql时间戳. [英] Converting a carbon date to mysql timestamp.

查看:78
本文介绍了将碳日期转换为mysql时间戳.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a timestamp variable column in a mysql database. Trying to convert a carbon timestamp to something that I can input there, but Carbon::now() only returns a Carbon object and when I try to use the timestamp string of the Carbon object, it does not register in 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 dump就像这样:

My first var dump is like so:

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 dump就是这样.

My second var dump is like so.

与"published_at"有关的mysql列是一个时间戳变量.我想如何将其从Carbon对象转换为?

The mysql column relating to "published_at" is a timestamp variable. How an I suppose to convert this from a Carbon Object?

先谢谢了.

推荐答案

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

You can also set Mutator on your model.

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

转换为时间戳

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

或者您可以使用strtotime

strtotime —将任何英语文本日期时间描述解析为Unix时间戳

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

希望获得帮助.

这篇关于将碳日期转换为mysql时间戳.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆