1. Home
  2. Computing & Technology
  3. PHP / MySQL

When Is Thanksgiving?
How to calculate Thanksgiving's date

By , About.com Guide

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:
<?php
//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

switch($day_of_week){
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.
More PHP / MySQL Quick Tips
Explore PHP / MySQL
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Step By Steps
  5. Calculate Thanksgiving Day with PHP

©2009 About.com, a part of The New York Times Company.

All rights reserved.