Home > Software design >  How to get next 15th minute of time from given time in PHP
How to get next 15th minute of time from given time in PHP

Time:10-29

Is there a way of getting the next 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

CodePudding user response:

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

CodePudding user response:

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