©
이 문서에서는 PHP 중국어 웹사이트 매뉴얼 풀어 주다
(PHP 5 >= 5.2.0, PHP 7)
DateTime::format -- DateTimeImmutable::format -- DateTimeInterface::format -- date_format — Returns date formatted according to given format
面向对象风格
$format
)$format
)$format
)过程化风格
$object
, string $format
)Returns date formatted according to given format.
object
仅为过程化风格:由 date_create() 返回的 DateTime 类型的对象。
format
Format accepted by date() .
Returns the formatted date string on success 或者在失败时返回 FALSE
.
Example #1 DateTime::format() example
面向对象风格
<?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
This method does not use locales. All output is in English.
[#1] ivannack at gmail dot com [2015-11-25 02:25:45]
Um exemplo com m??todos encadeados:
$time = '2015-11-24T23:52:45+0000';
echo DateTime::createFromFormat(DateTime::ISO8601, $time)->format('d/m/Y');
// retorna: 24/11/2015
echo DateTime::createFromFormat('Y-m-d', '2015-11-25' )->format('d/m/Y');
//retorna: 25/11/2015
[#2] prussell at cloudworksconsulting dot com [2014-09-15 12:02:05]
The udate function is a great start, but the formatting of the milliseconds is a little off. If it is within the first 100000 microseconds then the string will be less than 6 characters, so 0.012435 will appear as 0.12345. The revision below fixes this.
function udate($strFormat = 'u', $uTimeStamp = null)
{
// If the time wasn't provided then fill it in
if (is_null($uTimeStamp))
{
$uTimeStamp = microtime(true);
}
// Round the time down to the second
$dtTimeStamp = floor($uTimeStamp);
// Determine the millisecond value
$intMilliseconds = round(($uTimeStamp - $dtTimeStamp) * 1000000);
// Format the milliseconds as a 6 character string
$strMilliseconds = str_pad($intMilliseconds, 6, '0', STR_PAD_LEFT);
// Replace the milliseconds in the date format string
// Then use the date function to process the rest of the string
return date(preg_replace('`(?<!\\\\)u`', $strMilliseconds, $strFormat), $dtTimeStamp);
}
[#3] soul dot enforcer at gmail dot com [2014-04-02 12:48:39]
For full reference of the supported format character and results,
see the documentation of date() :
http://www.php.net/manual/en/function.date.php
[#4] chris at codewiz dot biz [2014-02-13 02:47:52]
I believe this is a bug but its note-worthy if it is intended (I am using PHP 5.5.3).
$ php --version
PHP Warning: Module 'xdebug' already loaded in Unknown on line 0
PHP 5.5.3-1ubuntu2.1 (cli) (built: Dec 12 2013 04:24:35)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies
What is happening:
DateTime()->format() will modify the timezone. So do not expect the public date property to be returned (format mask applied) based on the current public timezone property. format will decide that when calling ->format() it will use the server timezone which eliminates all usefulness of ->setTimezone().
<?php
$nowUtc = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
echo '$nowUtc'.PHP_EOL;
var_dump($nowUtc);
$nowUtc = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
echo '$nowUtc->format(\'Y-m-d h:i:s\')'.PHP_EOL;
var_dump($nowUtc->format('Y-m-d h:i:s'));
$nowUtc->setTimezone( new \DateTimeZone( 'Australia/Sydney' ) );
echo '$nowUtc->setTimezone( new \DateTimeZone( \'Australia/Sydney\' ) )'.PHP_EOL;
var_dump($nowUtc);
echo '$nowUtc->format(\'Y-m-d h:i:s\')'.PHP_EOL;
var_dump($nowUtc->format('Y-m-d h:i:s'));exit;
?>
outputs;
$nowUtc
object(DateTime)[2607]
public 'date' => string '2014-02-13 02:42:48' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
$nowUtc->format('Y-m-d h:i:s')
string '2014-02-13 02:42:48' (length=19)
$nowUtc->setTimezone( new \DateTimeZone( 'Australia/Sydney' ) )
object(DateTime)[2608]
public 'date' => string '2014-02-13 13:42:48' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Australia/Sydney' (length=16)
$nowUtc->format('Y-m-d h:i:s')
string '2014-02-13 01:42:48' (length=19) // expected 2014-02-13 13:42:48 based on Australia/Sydney - what is 2014-02-13 01:42:48 from anyway!
[#5] daysnine at gmail dot com [2013-11-04 11:02:55]
Seems like datetime::format does not really support microseconds as the documentation under date suggest it will.
Here is some code to generate a datetime with microseconds and timezone:
private function udate($format = 'u', $utimestamp = null) {
if (is_null($utimestamp))
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
echo udate('Y-m-d H:i:s.u T');
// Will output something like: 2014-01-01 12:20:24.42342 CET
[#6] Nads [2013-07-22 13:51:13]
Date and Time with split seconds
$micro_date = microtime();
$date_array = explode(" ",$micro_date);
$date = date("Y-m-d H:i:s",$date_array[1]);
echo "Date: $date:" . $date_array[0]."<br>";
[#7] craig dot constable at gmail dot com [2012-03-02 04:24:37]
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
?>
[#8] ca at agercon dot dk [2011-07-01 08:16:09]
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
?>
[#9] James Meyer [2011-01-06 11:49:12]
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.