'How to set time with DateTime modify?

Ok, we have 2018-04-09T10:00:00

How to set time in:

DateTime::createFromFormat('Y-m-d\TH:i:s', $periodauction)->modify('-1 day, +4 hour')->format('Y-m-d\TH:i:s')

It will return 2018-04-08T14:00:00, so it works fine.

But how to set specific time like 18 unstead +4 hour. To get 2018-04-08T18:00:00 ?

php


Solution 1:[1]

You can use the method setTime() to specify an hour and minutes:

$periodauction = '2018-04-09T10:00:00';
$date = DateTime::createFromFormat('Y-m-d\TH:i:s', $periodauction)->modify('-1 day');
$date->setTime(18,0);
echo $date->format('Y-m-d\TH:i:s');

Outputs:

2018-04-08T18:00:00

Solution 2:[2]

Set time.

$periodauction = '2018-04-09T10:00:00';

$date = DateTime::createFromFormat('Y-m-d\TH:i:s', $periodauction)->modify('-1 day t 18:00:00');

echo $date->format('Y-m-d\TH:i:s');

Or

$dateTime->modify('t 23:20:45');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Syscall
Solution 2 Obsidian