2

I have the datetimes:

2014-08-05 13:27:00
2014-08-05 13:29:00 
2014-08-05 13:36:00 
2014-08-05 13:38:00

How can I modify this with DateTime::modify() to be set at

2014-08-05 13:15:00
2014-08-05 13:15:00 
2014-08-05 13:30:00 
2014-08-05 13:30:00

(so, floor'ed on the last 15 minute mark..)

Sander
  • 1,402
  • 2
  • 21
  • 44
  • You should check this http://stackoverflow.com/questions/2480637/round-minute-down-to-nearest-quarter-hour – geoandri Oct 13 '14 at 10:02
  • time() returns the current time. if you do: $time=floor((time()/(15*60))*(15*60); that should do the trick as well ... – Gipsz Jakab Oct 13 '14 at 12:31

3 Answers3

5

Just calculate how many minutes you need to subtract, to get you wished result:

$dt = new DateTime('2014-08-05 13:27:00');
$min = $dt->format('i') % 15;
$dt->modify("-$min minutes");
echo $dt->format('Y-m-d H:i:s');

demo

Glavić
  • 42,781
  • 13
  • 77
  • 107
2

As you requested it to use DateTime::modify(), you could do it like this:

function roundToLastQuarterHour($date_string) {
    $date = new DateTime($date_string);

    // Remove any seconds, we don't need them
    $seconds = $date->format('s');
    $date->modify('-' . $seconds . ' seconds');

    // Store the original number of minutes on the datetime
    $original_minutes = $date->format('i');
    // Calculate how many minutes past the last quarter hour
    $remaining_minutes = $original_minutes - ($original_minutes - ($original_minutes % 15));

    // Modify the minutes, remove the number of minutes past the last quarter of an hour
    $date->modify('-' . $remaining_minutes . ' minutes');

    return $date;
}

roundToLastQuarterHour('2014-08-05 13:27:00')
// 2014-08-05 13:15:00
roundToLastQuarterHour('2014-08-05 13:29:00')
// 2014-08-05 13:15:00
roundToLastQuarterHour('2014-08-05 13:36:00')
// 2014-08-05 13:30:00
roundToLastQuarterHour('2014-08-05 13:38:00')
// 2014-08-05 13:30:00
SubjectCurio
  • 4,702
  • 3
  • 32
  • 46
0

this will do...

$adt = date("Y-m-d G:i:s",strtotime("2014-08-05 13:37:00")); // your desired time 
$min = date("i",strtotime("2014-08-05 13:37:00")); // taking the min out
// echo $adt;

if($min<15) $min =00; //changing the min value
elseif(15<$min && $min<30) $min =15;
elseif(30<$min && $min<45) $min =30;
elseif(45<$min && $min<=59) $min =45;

echo date("Y-m-d G:",strtotime($adt)).$min.date(":s",strtotime($adt)); //print or store it in a var

can also be made as a function if using more time values...

Ronser
  • 1,855
  • 6
  • 20
  • 38
  • You would need `elseif(45<$min && $min<=59)` as your last elseif statement, otherwise a date of something like `2014-08-05 13:59:33` wouldn't be rounded correctly. – SubjectCurio Oct 13 '14 at 10:27
  • i couldnt get you are you telling that the elseif at the last is not needed?? – Ronser Oct 13 '14 at 10:34
  • 1
    your last elseif checks if the minute is more than 45 and less than 59. If the minute was 59 though, it's not caught by this rule, so you could either change it to more than 45, and less than or equal to 59 i.e `(45 < $min && $min <= 59)` Or you could just do greater than 45 `(45 < $min)`, as the number of minutes in an hour probably won't change any time soon – SubjectCurio Oct 13 '14 at 12:06