Days until Christmas in PHP

Josh Sherman
1 min read
Software Development PHP

The holidays are upon us so it’s time for my obligatory holidays themed post! Keep in mind that the title may say Christmas but you could apply this same logic to any holiday or date of your choosing.

$christmas = date('Y-12-25');
$today     = date('Y-m-d');

echo (strtotime($christmas) - strtotime($today)) / 86400;

That’s all there is to it! This does happen to fall apart the day after the event, so if it’s a yearly recurring event, you will want to expand the logic a bit to check which year to use:

$christmas = date('Y-12-25');
$today     = date('Y-m-d');

if ($today > $christmas) {
    $christmas = date('Y-12-25', strtotime($christmas . ' +1 year'));
    $date1     = $christmas;
    $date2     = $today;
} else {
    $date1     = $today;
    $date2     = $christmas;
}

echo (strtotime($christmas) - strtotime($today)) / 86400;

Happy Holidays!!

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles