Change date format in PHP

Posted on

Question :

I’m trying to change the date format as follows:

 public function getDate() {
   return $date->DATE_FORMAT($date,'%b %d %Y %h:%i %p');

But I can not get the date in AM and PM format.


       public function getDate() {
   $date = new datetime($this->date);
    return $date->format('M d Y h:i A');


Answer :

Formatting for function date and a class DateTime () will be as follows:

$date = new DateTime('now');
echo $date->format('M d Y h:i A');


Nov 07 2014 03:03 PM

As with percentages, it looks like you’re using the strftime () function.

Formatting for strftime

%b - Mês abreviado (jan-dec)
%d - dia (01-31)
%Y - Ano com quatro digitos (2014)
%h - Mês abreviado conforme o locale (jan-dec)
%i - Hora representada de 01 a 12
%p - Adiciona AM ou PM

Date equivalent

M - Mês abreviado (jan-dec)
d - dia (01-31)
Y - Ano com quatro digitos (2014)
? - Não tem é possível formatar a data utlizando locale
h - Hora representada de 01 a 12
A - Adiciona AM ou PM

If you need to format the date using the locale, see this question .


Try the following:

public function getDate() {
   return $date->DATE_FORMAT($date,'%b %d %Y %a:%i %p');

Check out the W3schools


Leave a Reply

Your email address will not be published. Required fields are marked *