Skip to content

Instantly share code, notes, and snippets.

@Zegnat Zegnat/bday.php
Created Jun 26, 2017

Embed
What would you like to do?
<?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
You can’t perform that action at this time.