Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert hour to PM and AM with Carbon

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:00 or 20:00 with this extension how I can convert that to AM and PM format?

like image 218
Sredny M Casanova Avatar asked Dec 11 '15 17:12

Sredny M Casanova


1 Answers

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().

Example:

$now = Carbon::now();
echo $now->format('g:i A');

This will echo out something like 5:17 PM.

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: 12-hour format of an hour without leading zeros

i: Minutes with leading zeros

A: Uppercase Ante meridiem and Post meridiem

like image 51
Thomas Kim Avatar answered Nov 06 '22 09:11

Thomas Kim