downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DateTime::getLastErrors> <DateTime::diff
Last updated: Fri, 20 May 2011

view this page in

DateTime::format

(PHP 5 >= 5.2.0)

DateTime::formatDevuelve la fecha formateada según el formato dado

Descripción

Estilo orientado a objetos

public string DateTime::format ( string $format )

Estilo por procesos

string date_format ( DateTime $object , string $format )

Devuelve la fecha formateada según el formato dado.

Parámetros

object

Estilo por procesos solamente: Un objeto DateTime es devuelto por date_create()

format

Formato aceptado por date().

Valores devueltos

Devuelve la cadena de fecha formateada si se tuvo éxito o FALSE en caso de error.

Ejemplos

Example #1 Ejemplo de DateTime::format()

Estilo orientado a objetos

<?php
$fecha 
= new DateTime('2000-01-01');
echo 
$fecha->format('Y-m-d H:i:s');
?>

Estilo por procesos

<?php
$fecha 
date_create('2000-01-01');
echo 
date_format($fecha'Y-m-d H:i:s');
?>

El resultado del ejemplo sería:

2000-01-01 00:00:00

Notas

Este método no utiliza regiones. Todas las salidas son en inglés.

Ver también

  • date() - Dar formato a la fecha/hora local


add a note add a note User Contributed Notes DateTime::format
James Meyer 06-Jan-2011 07:49
A note about version differences - the results of this function differ significantly from php 5.2.x to 5.3.x . 

The 5.2 implementations will often parse to non-sensical values, such as:

1964/11-12: 1964/-99999/-99999
12/11-1964: -99999/12/11
12-31-1964: -99999/-99999/-99999
11121875: 1112/01/187
01321901: 0132/01/190

(this one makes sense, but was a poor guess)
31/12/1964: 1964/01/12

In 5.3+, these all come back as false, as I would expect.  5.2 was just a little optimistic about it's ability to parse dates, I guess.
eggerjohansi at gmail dot com 21-Apr-2010 04:04
if you want to use e.g. german weekdays you could add the following class as a temporary solution...
<?php
class DateTimeGerman extends DateTime {
    public function
format($format) {
       
$english = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
       
$german = array('Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag');
        return
str_replace($english, $german, parent::format($format));
    }
}
?>
it hasn't anything to do with nice programming but it works if you construct a
<?php
$dt
= new DateTimeGerman();
?>
instead of
<?php
$dt
= new DateTime();
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites