Skip to content

Instantly share code, notes, and snippets.

@Zegnat
Created June 26, 2017 15:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zegnat/cf911a6133830efdab3d6839b6918221 to your computer and use it in GitHub Desktop.
Save Zegnat/cf911a6133830efdab3d6839b6918221 to your computer and use it in GitHub Desktop.
<?php
// Create a DateTimeImmutable with the date and time to compare to.
// Use your current timezone, or the timezone of your visitor. I am in Sweden.
$now = new DateTimeImmutable('now', new DateTimeZone('Europe/Stockholm'));
// Create the object with your birthday.
$birthday = $now->setTimezone(new DateTimeZone('Europe/Amsterdam')); // Born in the Netherlands
$birthday = $birthday->setDate(1991, 10, 23); // On the 23rd of October, 1991
$birthday = $birthday->setTime(20, 9); // 9 minutes past 8 in the evening
// What year is it?
$currentyear = intval($now->format('Y'));
// My birthday this year.
// WARNING: need to enter your birth month and day again.
$nextbirthday = $birthday->setDate($currentyear, 10, 23);
$countdown = $now->diff($nextbirthday);
// If the countdown is negative, regenerate my next birthday to be next year.
if ($countdown->invert === 1) {
// WARNING: need to enter your birth month and day again.
$nextbirthday = $birthday->setDate($currentyear + 1, 10, 23);
$countdown = $now->diff($nextbirthday);
}
// Now $countdown is a DateInterval object (https://secure.php.net/manual/en/class.dateinterval.php)
// Get the amount of days until next birthday:
$days = $countdown->format('%a');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment