相关文章推荐
兴奋的开水瓶  ·  GradleException: ...·  1 年前    · 
个性的小马驹  ·  php mysql PDO的使用 ...·  1 年前    · 
Authentication Services
Command Line Specific Extensions
Compression and Archive Extensions
Cryptography Extensions
Database Extensions
Date and Time Related Extensions
File System Related Extensions
Human Language and Character Encoding Support
Image Processing and Generation
Mail Related Extensions
Mathematical Extensions
Non-Text MIME Output
Process Control Extensions
Other Basic Extensions
Other Services
Search Engine Extensions
Server Specific Extensions
Session Extensions
Text Processing
Variable and Type Related Extensions
Web Services
Windows Only Extensions
XML Manipulation
GUI Extensions
Keyboard Shortcuts
?
This help
Next menu item
Previous menu item
Previous man page
Next man page
Scroll to bottom
Scroll to top
Goto homepage
Goto search
(current page)
Focus search box
public DateTimeInterface::format ( string $format ): string
public DateTimeImmutable::format ( string $format ): string
public DateTime::format ( string $format ): string

过程化风格

按照指定格式返回格式化后的日期。 微秒。注意 date() 总是生成 000000 ,因为它需要一个 int 参数,而如果 DateTimeInterface 是使用微秒创建的,则 DateTimeInterface::format() 支持微秒。 示例: 654321 毫秒。与 u 的说明相同。 示例: 654 时区标识符 示例: UTC GMT Atlantic/Azores I (大写 i) 是否为夏令时 如果是夏令时为 1 ,否则为 0 。 跟格林尼治时间(GMT)的差异,小时和分钟时间没有冒号 示例: +0200 跟格林尼治时间(GMT)的差异,小时和分钟时间有冒号 示例: +02:00 P 相同,区别是使用 Z 替换 +00:00 返回(PHP 8.0.0 起可用) 示例: Z +02:00 如果知道会返回时区缩写,否则返回 GMT 时差。 示例: EST MDT +05 以秒为单位的时差。UTC 以西的时区为负的时差,以东为正的时差。 -43200 50400 完整日期/时间 ISO 8601 日期 2004-02-12T15:19:21+00:00 » RFC 2822 / » RFC 5322 格式化时间 示例: Thu, 21 Dec 2000 16:01:07 +0200 从 Unix 纪元(January 1 1970 00:00:00 GMT)到至今的秒数 参见 time()

面向对象风格

<?php
$date
= new DateTimeImmutable ( '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

示例 #2 更多示例

<?php
// 设置使用的默认时区。
date_default_timezone_set ( 'UTC' );

// 现在
$date = new DateTimeImmutable ();

// 打印类似:Wednesday
echo $date -> format ( 'l' ), "\n" ;

// 打印类似:Wednesday 19th of October 2022 08:40:48 AM
echo $date -> format ( 'l jS \o\f F Y h:i:s A' ), "\n" ;

/* 在 format 参数中使用常量 */
// 打印类似:Wed, 19 Oct 2022 08:40:48 +0000
echo $date -> format ( DateTimeInterface :: RFC2822 ), "\n" ;
?>
jurchiks101 at gmail dot com
6 months ago
If you want to get the week of year + year of said week, you need to use `format('o-W'), otherwise you can stumble into a non-obvious gotcha (unless you RTFM and memorised it, that is).
Using `Y` instead of `o` can result in incorrect year values in the case of the first or last week of the year (depending on if January 4th falls into said week or not), such as the first week of 2025 between 2024-12-30 and 2025-01-05 - `(new DateTime('2024-12-30'))->format('o-W')` will return the correct value of `2025-01` (as per ISO-8601 definition of week of year), while `format('Y-W')` will return `2024-01`.
Because of this, I would personally recommend avoiding using week of year anywhere unless absolutely necessary, as it is easy to make this mistake and never realise it.
joaopedrord2001 at gmail dot com
16 days ago
# If you want the return of data in the array to be formatted directly in the return.

public function toArray($keyName)
{
return [
'category' => $keyName,
'description_add' => $this->description,
'created_at' => $this->created_at->format('Y-m-d H:i:s'),
];
}

Return:
"result": {
"category": "DOCUMENTATION",
"description_add": "Rejected for internal reasons.",
"created_at": "2025-07-01 10:25:27"
}