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"
}