简体   繁体   English

Laravel中的日期格式

[英]Date formatting in Laravel

I have a form where I input 3 fields ( firstname , lastname , date ), my problem is in the date formatting. 我有一个输入3个字段( firstnamelastnamedate )的表单,我的问题是日期格式。

Here is an example. 这是一个例子。 I input the firstname , lastname and the date . 我输入firstnamelastnamedate

Preview of the form 表格预览

The date is well formatted => 01-02-1980 in the form. date格式01-02-1980 ,格式为=> 01-02-1980

However in my PhpMyAdmin, I have that => 318211200 it's the date : S 但是在我的PhpMyAdmin中,我有一个=> 318211200,它是dateS

Table in phpMyAdmin phpMyAdmin中的表

In my overview (panel), I also have 318211200 but not 01-02-1980 在我的概述(面板)中,我也有318211200但没有01-02-1980

Overview panel 概述面板

In my AdminController I have that: 在我的AdminController中,我有:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Student;

class AdminController extends Controller
{
    public function index(){
      $students = Student::oldest()->paginate(5);
      return view('admin.index', compact('students'))
             ->with('i', (request()->input('page',1)-1)*5);
    }

    public function store(Request $request)
    {

      $request->validate([
              'firstname' => 'required',
              'lastname' => 'required',
              'date' => 'required'
      ]);
      Student::create($request->all());
      return redirect()->route('admin.index')
                  ->with('success', 'save');
    }

}

And for my index.blade 对于我的index.blade

<thead>
                <tr>
                  <th>Firstname</th>
                  <th>Lastname</th>
                  <th>Date</th>
                </tr>
                </thead>
                @foreach($students as $student)
                @php
                  $date=date('d-m-Y', $student['date']);
                @endphp
                <tr>
                   <td> {{$student->firstname}}</td>
                   <td> {{$student->lastname}} </td>
                   <td> {{$student->date}} </td>

Do you have an idea please? 你有个主意吗? I thank you in advance 我提前谢谢你

The date column on the database table must be a datatime data type. 数据库表上的日期列必须是datatime数据类型。 So do any one of these two options: 因此,这两个选项中的任何一个都可以:

Option 1: In your migration file, change the code for the date column to 选项1:在您的迁移文件中,将“日期”列的代码更改为

$table->dateTime('date');

Then run the artisan command to rollback and update your database 然后运行artisan命令回滚并更新数据库

php artisan migrate:refresh

Then try submitting data from the form. 然后尝试从表单提交数据。

Option 2: 选项2:

Manually change the data type of the date field on the table from phpMyAdmin. 从phpMyAdmin手动更改表上date字段的数据类型。

You might have to truncate the database table, before submitting data from the form. 在从表单提交数据之前,您可能必须截断数据库表。

May be it is because you used the 'time' column type. 可能是因为您使用了“时间”列类型。 Try to define the column as 'date'. 尝试将列定义为“日期”。 In your migration file you should write sth like 在您的迁移文件中,您应该这样写:

$table->date('date');

Looks like you might have the wrong datatype for your date column. 看来您的date列数据类型可能不正确。 Consider using a datetime . 考虑使用datetime

If you wish you may alter the column on your table by: 如果希望,可以通过以下方式更改表格上的列:

ALTER TABLE students MODIFY date datetime;

Since you havent posted your migration, Im going to have to guess that you havent defined your date column in database is not defined to have dates as input. 由于您尚未发布迁移,因此我将不得不猜测您尚未在数据库中定义日期列,而未定义日期列为输入。 So, i recommend you to update your migration for this particular table with following code and refresh your migration . 因此,我建议您使用以下代码更新此特定表的迁移,并刷新

$table->date('date');

Now, you have a date enabled column to enter proper values. 现在,您有一个启用日期的列以输入正确的值。 After this, I recommend you to use carbon to have date format and other amazing stuff managed by Carbon If you are new to laravel, check this video out which will give you a basic understanding, its little old but you will get the idea. 在此之后,我建议您使用Carbon来设置日期格式和Carbon所管理的其他出色功能。如果您是laravel的新手, 请查看此视频 ,这将使您有一个基本的了解,虽然它有点旧,但您会明白的。

After you have done this, check this page where it has all possible out of the box options that you may use to manipulate dates. 完成此操作后, 请检查此页面 ,在此页面上可以使用开箱即用的所有选项,您可以使用这些选项来操纵日期。 Any issue, let me know 有什么问题,请告诉我

happy coding! 祝您编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM