D
D
Denis2018-04-22 11:57:40
PHP
Denis, 2018-04-22 11:57:40

How to make a multiple of 5 minutes in Datetime?

I receive time from other sites in this format

HH:MM DD/MM/YYYY

I will convert it like this
$advanceDeliveryDate="20:13 05/06/2018";    
$order_date = DateTime::createFromFormat('H:i d/m/Y', $advanceDeliveryDate);

Then all this is converted for writing to the database in DATETIME format
$save['OrderDate']=$order_date->format("Y-m-d H:i:s");

The question is actually that the time is a multiple of 5 minutes, how to do it? Can eat standard functions in DateTime? I searched but didn't find it.
The bottom line is that, as an example, the time has come 20:13, and the system took and made it a multiple of 5 minutes in a big way, that is, it will be 20:15
At first I thought to extract the minutes separately and do something like this But as practice has shown, it doesn't work when the time passed is 20:59
($number%5)?$number-$number%5+5:$number

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
Zhainar, 2018-04-22
@Virtus_pro

But as practice has shown, this does not work when the time is 20:59

I think everything works
$advanceDeliveryDate="20:59 05/06/2018";    
$order_date = DateTime::createFromFormat('H:i d/m/Y', $advanceDeliveryDate);

$minutes = $order_date->format('i');

if ($minutes % 5)
{
  $add = 5 - ($minutes % 5);
  $order_date->modify("+{$add} minutes");
}

var_dump($order_date);

D
D3lphi, 2018-04-22
@D3lphi

Work with timestamp:
$datetime->getTimestamp()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question