Andi Crew
Andi Crew

Reputation: 129

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 ?

Upvotes: 3

Views: 4136

Answers (2)

Garry Seldon
Garry Seldon

Reputation: 21

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');

Upvotes: 1

Syscall
Syscall

Reputation: 19780

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

Upvotes: 6

Related Questions