laravel 使用 Carbon 将小时转换为 PM 和 AM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34228745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert hour to PM and AM with Carbon
提问by Sredny M Casanova
I have a timestamp in PHP, so I'm using Carbon extension to manage everything that is related with date, time,etc. Now I have for example a hour 23:00or 20:00with this extension how I can convert that to AM and PM format?
我在 PHP 中有一个时间戳,所以我使用 Carbon 扩展来管理与日期、时间等相关的所有内容。例如,现在我有一个小时的23:00或20:00使用此扩展程序,我如何将其转换为 AM 和 PM 格式?
回答by Thomas Kim
I'm not sure if there is a out-of-the-box helper method for this, but you could always use the format
method, which defers to the base class method DateTime::format()
.
我不确定是否有一个开箱即用的辅助方法,但您始终可以使用该format
方法,该方法遵循基类 method DateTime::format()
。
Example:
例子:
$now = Carbon::now();
echo $now->format('g:i A');
This will echo out something like 5:17 PM.
这将呼应类似于下午 5:17 的内容。
The g
, i
, and A
can seem random, but the list of parameters can be found here: http://php.net/manual/en/function.date.php
的g
,i
和A
似乎是随机的,但参数列表可以在这里找到:http://php.net/manual/en/function.date.php
g
: 12-hour format of an hour without leading zeros
g
: 一个小时的 12 小时格式,没有前导零
i
: Minutes with leading zeros
i
: 带前导零的分钟
A
: Uppercase Ante meridiem and Post meridiem
A
: 大写的前子午线和后子午线