Change date format in PHP

Asked

Viewed 828 times

4

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’m not getting 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');
}
  • 1

    What is the format that returns $this->data ? put an example.

  • Returns this value: 2014-09-19 00:32:26 Now I wanted to change to American format.

  • 00 can be in AM PM or 24h format. As you know if it is in 12h or 24h format?

  • Using date()/Datetime or strftime() to format?

  • gives me the following error: Fatal error: Call to a Member Function DATE_FORMAT() on a non-object in C: xampp htdocs Prototype Phpclasses Post.php on line 117 sei k is in 24h format, because in the other examples I have prints in this format. I’m using the now function()

2 answers

3


The formatting for function date and the clase Datetime() be next:

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

Exit:

Nov 07 2014 03:03 PM

The way you’re with the percentages it looks like you’re using the function strftime().

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 that question.

  • +1 For it was here inattentive was not "a" instead of "h" but rather in addition to formatting.

  • Or a Dateinterval object that is worth a consideration to be made of Dateinterval::format() does not consider the format To as valid.

  • @Brunoaugusto, the Datetime class if I’m not mistaken uses the same date formatting().

  • 1

    Solution: public Function getDate() { $date = new datetime($this->date); Return $date->format(’M d Y h:i A'); } Thank you very much

  • @lost, Datetime yes, Dateinterval no. Maybe because formatting ranges does not require such a wide range of formats, you will understand...

0

Try the following:

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

Check out the example on W3schools

  • Rafael, php codes are not fired in snippets, only html, css and js. http://meta.pt.stackoverflow.com/questions/2115/presentndo-js-css-e-html-execut%C3%A1veis? cb=1

  • 2

    The intention was not to become an executable code here, it was just to be well formatted, I used wrong, I will pay more attention to post next time

  • Tranquil :) . To format in code just select the text and use the button { }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.