-2

Is there a way of getting the next hour's 15th minutes from given time?

For Example

  1. if input is 2021-08-26 12:00:37 then output should be 2021-08-26 12:15:00
  2. if input is 2021-08-26 12:30:37 then output should be 2021-08-26 13:15:00
  • I initially thought what @CBroe thought as well which would have made this a bit of an interesting question. However if you just want the next 15th minute of the hour it's a bit trivial. There's 2 possibilities either the time is before the 15th minute of this hour or after and it's trivial to generate the correct time given this – apokryfos Oct 29 '21 at 06:24
  • If the point is to get the date when it will be next 15 minutes (even if now it is 15 minutes and 1 second), this code will do it: https://3v4l.org/cZYfu – lis-dev Oct 29 '21 at 06:29
  • @moderator - The question is not answered in the linked post. At least not for someone asking such a basic question. The easiest way is to let DateTime handle the rollover of dates etc.: ```php $inputDate = new DateTimeImmutable('2021-08-26 12:00:37'); // implying you also want to turn 12:15 into 13:15 $newHour = $inputDate->format('H') + ($inputDate->format('i') >= 15 ? 1 : 0); $next15th = $inputDate->setTime($newHour, 15, 0); ``` (Would be more readable in an answer but was closed during writing... -.-) – Philipp Fritsche Oct 29 '21 at 06:30

3 Answers3

0

Use Carbon

It is installed by default in Laravel.

$date = Carbon::createFromFormat('Y-m-d H:i:s',  '2021-08-26 12:00:37'); 

$newDate = $date->addMinutes(15)->format('Y-m-d H:i:s');
Yash Parekh
  • 1,513
  • 2
  • 20
  • 31
  • Please share more details. How would this work for the second input string, where way more than 15 minutes should be added? – Nico Haase Nov 08 '21 at 13:31
-1

You can convert the date to a timestamp and then add it the equivalent of 15 minutes in seconds (15 * 60) and then convert it back to a date

MVT KVM
  • 168
  • 10
  • Please share more details. How would this work for the second input string, where way more than 15 minutes should be added? – Nico Haase Nov 08 '21 at 13:31
  • @NicoHaase in that case it would not work. When I responded the question was "How to get next 15th minute of time from given time in PHP" which is not the same. My answer not correct anymore – MVT KVM Nov 08 '21 at 13:46
  • As far as I see, the two example inputs and the corresponding expected outputs have been in the question since the initial state – Nico Haase Nov 08 '21 at 13:47
-1

I think this is what you looking for...

$start = '2018-05-21 20:24:45';
echo date('Y-m-d H:i:s',strtotime('+15 minutes',strtotime($start)));
Hashan
  • 178
  • 8