Converting a carbon date to mysql timestamp.(将碳日期转换为 mysql 时间戳.)
问题描述
我在 mysql 数据库中有一个时间戳变量列.试图将碳时间戳转换为我可以在那里输入的东西,但 Carbon::now() 只返回一个碳对象,当我尝试使用碳对象的时间戳字符串时,它不会在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 转储是这样的:
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 转储是这样的.
My second var dump is like so.
与published_at"相关的 mysql 列是一个时间戳变量.我想如何将其从碳对象转换?
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 时间戳.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将碳日期转换为 mysql 时间戳.
基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
