Search code examples
phpdatestrtotime

Add decimal years in php


I can add years to a date in PHP like

$yr = '2020-03-28';
$dt = date('d.m.Y', strtotime('+2 years'));
//Output 28.03.2022

How can I add decimal numbers of years in PHP? I want to do something like

$yr = '2020-03-28';
$dt = date('d.m.Y', strtotime('+1.25 years'));
//Output 01.01.1970

Thanks


Solution

  • Multiply it by 365, round down to the nearest integer, and add that many days, e.g.:

    $years = 1.25;
    $days = floor( $years * 365 ); // Note: not accurate for leap years!
    $dt = date('d.m.Y', strtotime("+$days days"));