Как нормально обнулить секунды в DateTime?
Придумал два варианта. Оба с запашком. Третий вариант, по аналогии с датой, не работает.
<?php
html::textPlainHeader();
$dt = new DateTime();
print_r((clone $dt)->setTimestamp(intdiv($dt->getTimestamp(), 60) * 60));
$dt = new DateTime();
print_r($dt->modify('-'.$dt->format('s').' sec')->modify('-'.$dt->format('u').' usec'));
$dt = new DateTime();
print_r($dt->modify('first second of this minute')); // по аналогии с 'first day of this month'
/*
Warning: DateTime::modify(): Failed to parse time string (first second of this minute) at position 13 (o):
The timezone could not be found in the database in /var/proj/bk/www/2.php on line 14
Call Stack:
0.0960 1588064 1. {main}() /var/proj/bk/www/2.php:0
0.1020 1882280 2. DateTime->modify($modify = 'first second of this minute') /var/proj/bk/www/2.php:14
*/
DateTime Object
(
[date] => 2024-02-02 14:22:00.000000
[timezone_type] => 3
[timezone] => Etc/GMT-3
)
DateTime Object
(
[date] => 2024-02-02 14:22:00.000000
[timezone_type] => 3
[timezone] => Etc/GMT-3
)
Warning: DateTime::modify(): Failed to parse time string (first second of this minute) at position 13 (o): The timezone could not be found in the database in /var/proj/bk/www/2.php on line 19
Call Stack:
0.0996 1587472 1. {main}() /var/proj/bk/www/2.php:0
0.1018 1881688 2. DateTime->modify($modify = 'first second of this minute') /var/proj/bk/www/2.php:19
phpinfo()
Default timezone Etc/GMT-3