如何在PHP中使午夜工作以截断时间?

huangapple go评论64阅读模式
英文:

How to make midnight work in PHP for truncating time?

问题

This doesn't set the time to midnight:

new DateTime('first day of -1 month midnight')

It's the same as if midnight weren't there.
Tested in PHP 7.3.33 and PHP 8.2.4.

The documentation of time formats isn't very clear. But even if I take an example straight from it, the behavior differs:

echo (new DateTime('yesterday noon'))->format('Y-m-d h:m');

doesn't output noon, the minute field is non-zero.

I've changed the above back to talk about minutes. Which can be accessed with i, not m. I attempted to correct it, but, as the comments show, I did it incorrectly, leading to confusion. 12 is indeed noon both with h and H. I meant to refer to the hour being 12 at midnight.

英文:

This doesn't set the time to midnight:

new DateTime('first day of -1 month midnight')

It's the same as if midnight weren't there.
Tested in PHP 7.3.33 and PHP 8.2.4.

The documentation of time formats isn't very clear. But even if I take an example straight from it, the behaviour differs:

echo (new DateTime('yesterday noon'))->format('Y-m-d h:m');

doesn't output noon, the minute field is non-zero.

I've changed the above back to talk about minutes. Which can be accessed with i, not m. I attempted to correct it, but, as the comments show, I did it incorrectly, leading to confusion. 12 is indeed noon both with h and H. I meant to refer to the hour being 12 at midnight.

答案1

得分: -2

h 提供从 01 到 12 的值

H 提供从 00 到 23 的值,应在此处使用

英文:

h gives values from 01 to 12

H gives values from 00 to 23 and should be used here

huangapple
  • 本文由 发表于 2023年5月11日 20:26:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定