Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get date for holidays using php

Tags:

For example the:

  1. 3rd Monday of January for Martin Luther King Day
  2. 3rd Monday of February for Presidents' Day (Washington's Birthday)
  3. Last Sunday of March for Easter
  4. Last Monday of May for Memorial Day

I am trying to get these dates, so that I can mark it on my calendar without manually putting everything for the years to come.


MODIFIED ANSWER!!

$curYir = date("Y");//current year  $MLK = date('Y-m-d', strtotime("january $curYir third monday")); //marthin luthor king day $PD = date('Y-m-d', strtotime("february $curYir third monday")); //presidents day $Est =  date('Y-m-d', easter_date($curYir))); // easter  $MDay = date('Y-m-d', strtotime("may $curYir first monday")); // memorial day //("may $curYir last monday") will give you the last monday of may 1967 //much better to determine it by a loop       $eMDay = explode("-",$MDay);       $year = $eMDay[0];       $month = $eMDay[1];       $day = $eMDay[2];        while($day <= 31){           $day = $day + 7;       }       if($day > 31)       $day = $day - 7;        $MDay = $year.'-'.$month.'-'.$day; $LD = date('Y-m-d', strtotime("september $curYir first monday"));  //labor day $CD = date('Y-m-d', strtotime("october $curYir third monday")); //columbus day $TH = date('Y-m-d', strtotime("november $curYir first thursday")); // thanks giving  //("november $curYir last thursday") will give you the last thursday of november 1967 //much better to determine it by a loop       $eTH = explode("-",$TH);       $year = $eTH[0];       $month = $eTH[1];       $day = $eTH[2];        while($day <= 30){           $day = $day + 7;       }       if($day > 30)       //watch out for the days in the month November only have 30       $day = $day - 7;        $TH = $year.'-'.$month.'-'.$day; 
like image 600
telexper Avatar asked Feb 16 '13 06:02

telexper


People also ask

How to get holidays in PHP?

Yasumi (Japanese for 'Holiday'「休み」) is the easy PHP library that helps you retrieve the dates and names of holidays and other special celebrations from various countries/states. It is calculation and rule driven avoiding the need of a comprehensive database.

How does PHP calculate working days?

function add_business_days($startdate,$buisnessdays,$holidays,$dateformat){ $i=1; $dayx = strtotime($startdate); while($i < $buisnessdays){ $day = date('N',$dayx); $date = date('Y-m-d',$dayx); if($day < 6 && ! in_array($date,$holidays))$i++; $dayx = strtotime($date.


2 Answers

You can leverage this function of php. strtotime

$currentYear = date("Y"); 

MLK Day -

echo date('Y-m-d', strtotime("third monday of january $currentYear")); 

Presidents Day -

echo date('Y-m-d', strtotime("third monday/OD February $currentYear")); 

Easter -

echo date('Y-m-d', strtotime("last sunday of march $currentYear")); 

Memorial Day -

echo date('Y-m-d', strtotime("last monday of may $currentYear")); 
like image 145
Sanchit Avatar answered Oct 29 '22 09:10

Sanchit


The strtotime function is useful here but it seems to have some peculiarities on how it interprets the English verbiage. Be sure to check your wording carefully and check your results.

For instance this seems like it should work, but it will return the last Monday in April ($currentYear = 2014)!

echo "Memorial Day " . date('F d, Y', strtotime("may $currentYear last monday")) . "<br />"; 

However, this phrasing will return the correct holiday for 2014.

echo "Memorial Day " . date('F d, Y', strtotime("last monday of May $currentYear")) . "<br />";  

And this will give you the correct date for the holiday next year.

echo "Memorial Day " . date('F d, Y', strtotime("last monday of May $currentYear+1")) . "<br />"; 

The PHP doc page has additional comments on its use. Take care

like image 24
Steve Isenberg Avatar answered Oct 29 '22 10:10

Steve Isenberg