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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32220091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:51:15  来源:igfitidea点击:

Converting a carbon date to mysql timestamp.

phpmysqllaraveltimestampphp-carbon

提问by 1N5818

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.

我在 mysql 数据库中有一个时间戳变量列。尝试将 carbon 时间戳转换为我可以在那里输入的内容,但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);   
}

My first var dump is like so:

我的第一个 var dump 是这样的:

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)  

My second var dump is like so.

我的第二个 var dump 就是这样。

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

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

Thanks in advance.

提前致谢。

采纳答案by mdamia

You can also set Mutator on your model.

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

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

to convert to timestamp

转换为时间戳

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

or you can just convert the date to timestamp using strtotime

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

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

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

Hope this help.

希望这有帮助。

回答by orrd

The short answer is that toDateTimeString()is what you're looking for:

简短的回答是,这toDateTimeString()就是你要找的:

$input['published_at'] = Carbon::now()->toDateTimeString();

See http://carbon.nesbot.com/docs/for more options, including toDateString()if you just want the date part and not the time.

有关更多选项,请参阅http://carbon.nesbot.com/docs/,包括toDateString()您是否只需要日期部分而不是时间。

But an even better way to handle it would be to let Laravel handle casting the date value to/from a Carbon object for you. See https://laravel.com/docs/5.4/eloquent-mutators#date-mutators.

但是处理它的更好方法是让 Laravel 为您处理向/从 Carbon 对象转换日期值。请参阅https://laravel.com/docs/5.4/eloquent-mutators#date-mutators

回答by iCoders

The problem is in your date string, for example, you have this:

问题出在您的日期字符串中,例如,您有:

public function setCrbDateAttribute($value)
{
     $this->attributes['crb_date'] = \Carbon\Carbon::createFromFormat('d-m-Y h:i', $value);
}

Now, if there is a date like 10-12-2014 then this error will occur because the hour and minute is missing. So you make sure that the date contains all the pars and also make sure that the date string contains - as a separator not /.

现在,如果有一个像 10-12-2014 这样的日期,那么就会发生这个错误,因为缺少小时和分钟。因此,您要确保日期包含所有解析,并确保日期字符串包含 - 作为分隔符而不是 /。

In other words, check the $value before you use Carbon and make sure your date string contains exactly the same formatted string you've used in the method.

换句话说,在使用 Carbon 之前检查 $value 并确保您的日期字符串包含与您在方法中使用的格式完全相同的字符串。

This also happens in an accessor method, so check the date value first before you use it in Carbon::createFromFormat().

这也发生在访问器方法中,因此在使用它之前先检查日期值 Carbon::createFromFormat().

If you are getting the date from user input then validate the date before using it using date or date_format:format rule, check the validation here.

如果您从用户输入中获取日期,然后在使用日期或 date_format:format 规则之前验证日期,请在此处检查验证。

Answer ref:

回答参考:

Laravel/Carbon Timestamp 0000-00-00 00:00:00 or Unexpected data found. Unexpected data found. Data missing

Laravel/Carbon Timestamp 0000-00-00 00:00:00 或发现意外数据。发现意外数据。数据缺失