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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:48:17  来源:igfitidea点击:

Convert hour to PM and AM with Carbon

phplaravelphp-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:0020: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 formatmethod, 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 Acan seem random, but the list of parameters can be found here: http://php.net/manual/en/function.date.php

giA似乎是随机的,但参数列表可以在这里找到: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: 大写的前子午线和后子午线