Home  >  Q&A  >  body text

How to make midnight work in PHP to truncate time?

This will not set the time to midnight:

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

As if midnight does not exist. Tested in PHP 7.3.33 and PHP 8.2.4.

The documentation for the time format is not very clear. But even if I take an example directly from it, the behavior will be different:

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

Do not output noon, the minute field is non-zero.

I changed the above back to talking about minutes. Can be accessed using i instead of m. I tried to correct it, but, as the comments indicate, I did it incorrectly, causing confusion. 12 is indeed the noon of H and H. I mean 12 midnight.

P粉288069045P粉288069045172 days ago319

reply all(1)I'll reply

  • P粉545218185

    P粉5452181852024-04-04 09:20:50

    h gives values ​​from 01 to 12

    H gives values ​​from 00 to 23, should be used here

    reply
    0
  • Cancelreply