w3resource

PHP Date Exercises : Count the number of days between current day and birthday

PHP date: Exercise-2 with Solution

Create a simple 'birthday countdown' script, the script will count the number of days between current day and birthday.

Sample Solution:

PHP Code:

<?php
$target_days = mktime(0,0,0,12,31,2013);// modify the birth day 12/31/2013
$today = time();
$diff_days = ($target_days - $today);
$days = (int)($diff_days/86400);
print "Days till next birthday: $days days!"."\n";
?>

Sample Output:

Days till next birthday: -1143 days!

Flowchart :

Flowchart: Count the number of days between current day and birthday

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script which will display the copyright information in the following format. To get current year you can use the date() function.
Next: Write a PHP script to print the current date in the following format. To get current date's information you can use the date() function.

What is the difficulty level of this exercise?



PHP: Tips of the Day

SQL injection that gets around mysql_real_escape_string()

Consider the following query:

$iId = mysql_real_escape_string("1 OR 1=1");    
$sSql = "SELECT * FROM table WHERE id = $iId";

mysql_real_escape_string() will not protect you against this. The fact that you use single quotes (' ') around your variables inside your query is what protects you against this. The following is also an option:

$iId = (int)"1 OR 1=1";
$sSql = "SELECT * FROM table WHERE id = $iId";

Ref : https://bit.ly/32q3bJ7