PHP Date Exercises : Count the number of days between current day and birthday
2. Birthday Countdown
Create a simple 'birthday countdown' script, the script will count the number of days between current day and birthday.
Sample Solution:
PHP Code:
<?php
// Set the target date for the birthday using mktime() function: December 31, 2013
$target_days = mktime(0,0,0,12,31,2013);
// Get the current timestamp
$today = time();
// Calculate the difference in seconds between the target date and today's date
$diff_days = ($target_days - $today);
// Convert the difference in seconds to days
$days = (int)($diff_days/86400);
// Print the number of days until the next birthday
print "Days till next birthday: $days days!"."\n";
?>
Output:
Days till next birthday: -1143 days!
Explanation:
The above PHP code calculates the number of days until a specific target date (December 31, 2013) from the current date.
It uses the "mktime()" function to create a timestamp representing the target date, then subtracts this timestamp from the current timestamp obtained using "time()". The difference is converted into days by dividing it by the number of seconds in a day (86400).
Finally, it prints out the number of days until the specified date.
Flowchart :

For more Practice: Solve these Related Problems:
- Write a PHP script that calculates the remaining days until a user-specified birthday and handles leap year birthdays correctly.
- Write a PHP function to display a countdown timer that updates every second using AJAX for a birthday event.
- Write a PHP program to output the number of days, hours, and minutes remaining until the next birthday from today's date.
- Write a PHP script that calculates the birthday countdown and displays an encouraging message as the date approaches.
Go to:
PREV : Copyright Info Display.
NEXT : Current Date Formatting.
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.