Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to convert days into human-readable years, months and days in PHP

2 Answers

0 votes
/*
Difference between Jan 1, 2024 and Mar 27, 2025 (including both days):
1 years 2 months 27 days
or 14 months 27 days
or 64 weeks 4 days
or 452 calendar days
*/

$days = 452;
$date = '2024-01-01'; // based on actual days in each month

$start_date = new DateTime($date);
$end_date = (new DateTime($date))->add(new DateInterval("P{$days}D") );

$dd = date_diff($start_date, $end_date);

echo $dd->y." years ".$dd->m." months ".$dd->d." days";

  
     
/*
run: 
  
1 years 2 months 27 days
  
*/

 



answered Jun 26, 2024 by avibootz
0 votes
// This conversion is based on the average number of days in a year, 
// which is about 365.2425 days.

function daysToYMD(int $days): string {
    $start = new DateTime("1970-01-01");
    $end   = (clone $start)->modify("+$days days");

    $diff = $start->diff($end);

    return sprintf(
        "%d year%s, %d month%s and %d day%s",
        $diff->y, $diff->y === 1 ? "" : "s",
        $diff->m, $diff->m === 1 ? "" : "s",
        $diff->d, $diff->d === 1 ? "" : "s"
    );
}

echo daysToYMD(452) . PHP_EOL;




/*
run:

1 year, 2 months and 28 days

*/

 



answered Dec 31, 2025 by avibootz

Related questions

...