Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

datetime

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

For example:

  • it's set to 2012-10-12 10:30:00
  • it should become 2012-11-22 10:30:00

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?

like image 567
insertusernamehere Avatar asked Nov 22 '12 00:11

insertusernamehere


1 Answers

Not much better:

$newDate = new DateTime('today '.$date->format('H:i'));
like image 142
jaudette Avatar answered Oct 17 '22 14:10

jaudette