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

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?



Solution 1:[1]

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.

Solution 2:[2]

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'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jspit
Solution 2 Suraj Sanwal