Days until Christmas in 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!!

Josh Sherman - The Man, The Myth, The Avatar

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.


If you found this article helpful, please consider buying me a coffee.