简体   繁体   English

以正确的格式在laravel中显示日期时间

[英]Displaying datetime in laravel in the right format

I have a blog created with Laravel 5.5, In PostController and in Create function i have specified the time format for "Published_at" cloumn to 24 format, and it's inserted correctly into database with the right format 我有一个用Laravel 5.5创建的博客,在PostController和Create函数中,我已将“ Published_at” cloumn的时间格式指定为24格式,并且已正确插入具有正确格式的数据库中

Nov 30, 2017 | 17:11:19

But when i try to display the time in post page i got this format 但是当我尝试在帖子页面中显示时间时,我得到了这种格式

30, 11, 2017 | 1:51:29 pm

I don't know why, i have tried to use 我不知道为什么,我尝试使用

date('M j,Y | H:i:s' ,strtotime($post->published_at)) 

solution but i got wrong date and time 解决方案,但我输入了错误的日期和时间

Jan 1,1970 | 01:00:00

I hope the problem clearly described. 希望问题能清楚地描述。 This is the code used to validate the date format in store function 这是用于验证存储功能中日期格式的代码

'published_at' => [
            'required',
            'regex:/^[\d]{2},\s[\d]{2},\s[\d]{4}\s\|\s[\d]{2}:[\d]{2}:[\d]{2}$/',
        ],

And in create page the date display correctly 并且在创建页面中日期显示正确

<input type="text" class="form-control" id="published_at" name="published_at" value="{{ old('published_at', date('M j, Y | G:i:s')) }}" autofocus>

But in Other pages when i use {{ $post->published_at }} or {{ $post->created_at }}... the time display in 12 format 但是在其他页面中,当我使用{{$ post-> published_at}}或{{$ post-> created_at}} ...时,时间以12格式显示

更新的代码

echo DateTime::createFromFormat('d, m, Y \| h:i:s A', $post->published_at)->format('M j,Y | H:i:s'); 

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

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