默认情况下,当Laravel创建帖子时,它将日期和时间戳记存储为: 2019-01-31 23:32:06
我怎样才能显示它的视图 - 2018年1月31日下午11点32分
答案 0 :(得分:0)
U可以使用Php Carbon来修改日期。例如。
$dt = Carbon::createFromFormat('Y-m-d H:i:s.u', '2019-02-01 03:45:27.612584');
// $dt->toAtomString() is the same as $dt->format(DateTime::ATOM);
echo $dt->toAtomString(); // 2019-02-01T03:45:27+00:00
echo $dt->toCookieString(); // Friday, 01-Feb-2019 03:45:27 UTC
echo $dt->toIso8601String(); // 2019-02-01T03:45:27+00:00
// Be aware we chose to use the full-extended format of the ISO 8601 norm
// Natively, DateTime::ISO8601 format is not compatible with ISO-8601 as it
// is explained here in the PHP documentation:
// https://php.net/manual/class.datetime.php#datetime.constants.iso8601
// We consider it as a PHP mistake and chose not to provide method for this
// format, but you still can use it this way:
echo $dt->format(DateTime::ISO8601); // 2019-02-01T03:45:27+0000
echo $dt->toISOString(); // 2019-02-01T03:45:27.612584Z
echo $dt->toJSON(); // 2019-02-01T03:45:27.612584Z
echo $dt->toIso8601ZuluString(); // 2019-02-01T03:45:27Z
echo $dt->toDateTimeLocalString(); // 2019-02-01T03:45:27
echo $dt->toRfc822String(); // Fri, 01 Feb 19 03:45:27 +0000
echo $dt->toRfc850String(); // Friday, 01-Feb-19 03:45:27 UTC
echo $dt->toRfc1036String(); // Fri, 01 Feb 19 03:45:27 +0000
echo $dt->toRfc1123String(); // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toRfc2822String(); // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toRfc3339String(); // 2019-02-01T03:45:27+00:00
echo $dt->toRfc7231String(); // Fri, 01 Feb 2019 03:45:27 GMT
echo $dt->toRssString(); // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toW3cString(); // 2019-02-01T03:45:27+00:00
请详细了解Php Carbon
答案 1 :(得分:0)
您可以在laravel中使用Carbon。
首先,使用carbon
:
use Carbon\Carbon;
然后使用您想要的格式,就像您这样:
public function test()
{
$dt = User::find(1);
return $dt->created_at->format('M jS\\, Y h:i:s A'); //Dec 1st, 1975 02:15:16 PM
}
您也可以在视图中使用它,只需在文件顶部使用carbon
:
<?php use Carbon\Carbon;?>
然后更改显示格式,例如{{$dt->created_at->format('M jS\\, Y h:i:s A')}}
答案 2 :(得分:0)
对不起,答案太晚了
您的输入是2018-01-31 23:32:06
您的期望输出是Jan 31st, 2018 11:32pm
我创建了一个函数
function convertToPreferedDateFormat($dateString='')
{
$toTimeString = strtotime($dateString);
$yourExpectedFormat = 'M dS, Y h:ia';
// $prettyBeautifullWay = 'l jS \of F Y h:i:s A';
return date($yourExpectedFormat, $toTimeString);
}
然后进行测试
$timeString = '2018-01-31 23:32:06';
$expectedOutput = 'Jan 31st, 2018 11:32pm';
echo convertToPreferedDateFormat($timeString);
echo "<br>";
echo $expectedOutput;