1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

When Is Thanksgiving?

How to calculate Thanksgiving's date


Although there is no specific PHP function to give you the date of Thanksgiving in any given year, we know that in the USA it always falls on the forth Thursday in November. Using this information we can calculate the actual date using the script below:
 //This gets today's date 
 $date =time () ;

 //This makes the current year a variable 
 $year = date('Y', $date) ;

 //Here we generate the first day of November
 $first_day = mktime(0,0,0,11, 1, $year) ;

 //We determine what day of the week the first falls on
 $day_of_week = date('D', $first_day) ;

 //Based upon this, we add the appropriate number of days to get to the forth Thursday of the month

 case "Sun": $add = 25; break;
 case "Mon": $add = 24; break; 
 case "Tue": $add = 23; break; 
 case "Wed": $add = 22; break; 
 case "Thu": $add = 21; break; 
 case "Fri": $add = 27; break; 
 case "Sat": $add = 26; break; 

 $Thanks = 1 + $add;

 echo "This year, $year, Thanksgiving Day falls on 11/$Thanks.";
If you want to calculate when Thanksgiving falls for any other year you can modify the $date =time () ; line to use the mktime () function to generate a date in any year you wish.
Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Step By Steps
  5. Calculate Thanksgiving Day with PHP

©2014 About.com. All rights reserved.