php - 转换 DateTime 时遇到格式不正确的数值

标签 php datetime laravel fullcalendar

我在 Angular 中使用 fullCalendar 插件/指令,目前在尝试将日期/时间保存到数据库中时遇到问题。

这些是发布到我的服务器的值:

{"title":"Hey","start":"2015-08-13T00:00:00.000Z","end":"2015-08-13T00:00:00.000Z","allDay":true}

现在,在我的 Controller 中,我尝试在保存到数据库之前将日期/时间字符串转换为有效的日期/时间格式:

public function store(ScheduleRequest $request)
{
    $schedule = new Schedules;
    $schedule->allDay = $request->allDay;
    $schedule->start = strtotime(date('Y-m-d H:i:s', $request->start));
    $schedule->end = strtotime(date('Y-m-d H:i:s', $request->end));
    $schedule->title = $request->title;

    if ($schedule->save())
    {
        return [
            'success' => 'Data Was Saved Successfully'
        ];
    }
}

这是我得到的错误:

A non well formed numeric value encountered

我想知道如何使用指定的格式将两个日期时间值转换为 PHP 中的有效日期时间对象。

最佳答案

strtotime 是将字符串转换为时间戳,date 是将时间戳转换为字符串,您需要使用 strtotime 反转日期,如下所示:

public function store(ScheduleRequest $request)
{
    $schedule = new Schedules;
    $schedule->allDay = $request->allDay;
    $schedule->start = date('Y-m-d H:i:s', strtotime($request->start));
    $schedule->end = date('Y-m-d H:i:s', strtotime($request->end));
    $schedule->title = $request->title;

    if ($schedule->save())
    {
        return [
            'success' => 'Data Was Saved Successfully'
        ];
    }
}

关于php - 转换 DateTime 时遇到格式不正确的数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31945156/

相关文章:

php - 为什么我不应该在 PHP 中使用 mysql_* 函数?

php - 从网站上的数据库中删除图像

javascript - 在 ajax 调用的脚本中调用 ajax 未正确运行 (PHP)

MySQL 日期时间 : insert a date with timezone offset

Excel PowerQuery - 以正确的日期时间转换导入 SQL Server 日志的最佳方式?

php - 我怎样才能找出 mysqli 扩展有什么问题?

ruby-on-rails - 如何在 Rails 中设置日期时间格式

Laravel @yield 显示带有 HTML 标签的内容

php - 调用模型 [App\Team] laravel 6 上的未定义关系 [users]

javascript - Laravel + React : Pass Function in JSON response