K
K
KirillTrueno2021-12-17 17:43:05
PHP
KirillTrueno, 2021-12-17 17:43:05

How to add several days (days) to a date with an accuracy of up to a minute?

There is a string with a date and time, you need to add a few days to it, but there is a problem with adding minutes.

$old_date = '2022-01-15 23:59:59';

$date = new DateTime($old_date);
$date->modify('+10 day');
$new_date = $date->format('Y-m-d H:m:s');

print($new_date);
// Выводит 2022-01-25 23:01:59

Those. I add 10 days, but actually 9 days 23 hours 2 minutes have been added.
Where did 58 minutes go?
How to add several days (24 hours) to a date?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-12-17
@KirillTrueno

You instead of minutes deduced a month.
$new_date = $date->format('Y-m-d H:i:s');

T
tempick, 2021-12-17
@tempick

Didn't work with DateTime, don't know why such date is obtained. But this way works correctly:

$timestamp = strtotime($old_date);
$newTimestamp = $timestamp + 60*60*24*10;
$new_date = date('Y-m-d H:i:s',$newTimestamp);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question