Change date format in PHP

Posted on

Coding Problem :

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

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

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

solution:

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

    

asked by anonymous 07.11.2014 / 14:55

Answer :


Answer 1 :

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');

Output:

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 .

    


Answer 2 :

Try the following:

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

Check out the W3schools

    


Answer 3 :


Answer 4 :


Answer 5 :


Answer 6 :


Answer 7 :


Answer 8 :


Answer 9 :

Leave a Reply

Your email address will not be published.