insertusernamehere
insertusernamehere

Reputation: 23610

Is there a short way to modify DateTime to "today" but keep the previous stored time?

There's a DateTime-object. I want to set it to today and keep the time.

For example:

This won't work of course:

// this obviously changes it to 2012-11-22 00:00:00
$date->modify('today'));

This will work, but seems like a little much effort:

$clone = clone $date();
$date->modify('today')->setTime($clone->format('H'), $clone->format('i'));

Is there a shorter/more efficient way?

Upvotes: 6

Views: 2249

Answers (1)

jaudette
jaudette

Reputation: 2303

Not much better:

$newDate = new DateTime('today '.$date->format('H:i'));

Upvotes: 7

Related Questions