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
[edit] Last updated: Fri, 25 May 2012

view this page in

DateTime::format

date_format

(PHP 5 >= 5.2.0)

DateTime::format -- date_format指定した書式でフォーマットした日付を返す

説明

オブジェクト指向型

public string DateTime::format ( string $format )

手続き型

string date_format ( DateTime $object , string $format )

指定した書式でフォーマットした日付を返します。

パラメータ

object

手続き型のみ: date_create() が返す DateTime オブジェクト

format

date() が理解できる書式指定文字列。

返り値

成功した場合にフォーマット済みの日付文字列、失敗した場合に FALSE を返します。

例1 DateTime::format() の例

オブジェクト指向型

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

手続き型

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

上の例の出力は以下となります。

2000-01-01 00:00:00

注意

このメソッドはロケールを考慮しません。出力結果はすべて英語となります。

参考

  • date() - ローカルの日付/時刻を書式化する


DateTime::getLastErrors> <DateTime::diff
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes DateTime::format
craig dot constable at gmail dot com 02-Mar-2012 04:24
Using a datetime field from a mysql database e.g. "2012-03-24 17:45:12"

<?php

$result
= mysql_query("SELECT `datetime` FROM `table`");
$row = mysql_fetch_row($result);
$date = date_create($row[0]);

echo
date_format($date, 'Y-m-d H:i:s');
#output: 2012-03-24 17:45:12

echo date_format($date, 'd/m/Y H:i:s');
#output: 24/03/2012 17:45:12

echo date_format($date, 'd/m/y');
#output: 24/03/12

echo date_format($date, 'g:i A');
#output: 5:45 PM

echo date_format($date, 'G:ia');
#output: 05:45pm

echo date_format($date, 'g:ia \o\n l jS F Y');
#output: 5:45pm on Saturday 24th March 2012

?>
ca at agercon dot dk 01-Jul-2011 08:16
The date_format can be use to get the last day of February:

<?php

function last_day_of_feb ($year) {
# The 0th day of a month is the same as the last day of the month before
       
$ultimo_feb_str = $year . "-03-00";
       
$ultimo_feb_date = date_create($ultimo_feb_str);
       
$return = date_format($ultimo_feb_date, "Y-m-d");
        return
$return;
}

echo
last_day_of_feb(2011) . "\n"; # 2011-02-28
echo last_day_of_feb(2012) . "\n"; # 2011-02-29

?>
James Meyer 06-Jan-2011 11: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 09: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