Home > OS >  How to add an odd day to a time in PHP?
How to add an odd day to a time in PHP?

Time:04-05

How to add an odd day to a time in PHP?

Like add 1.5 day? Any idea?

When I try like 1.5 or 1,5 then it's adding 15 days.

Is there anyway to add odd day to time?

CodePudding user response:

Following is the one approach you can do first add an exact number of days(non-fractional part) and then add fractional value in hours.

For example, If you want to add 1.5 days then add 1 day and 12 hours.

echo date("Y-m-d H:i:s", strtotime(' 1 Day  12 Hours'))

CodePudding user response:

If there are fractions of days, they can be converted to seconds and then added.

$days = 1.5;
$seconds = (int)(86400 * $days);

$dt = new Datetime('2022-04-01');

$dt->modify($seconds.' Seconds'); //add

echo $dt->format('Y-m-d H:i');
//2022-04-02 12:00

Also works with $days = 1.25; This then returns 2022-04-02 06:00 as a result. Or $days = 1.1 returns 2022-04-02 02:24.

  • Related