PHP Calendar Functions

    PHP supports a number of calendar functions that will allow you to simplify the conversion between the different calendar formats. All the conversions are being done based on the Julian day count which starts from January 1st, 4713 B.C. so if you want to convert between different calendar system then you have to convert it to Julian day count and then convert it to your choice of the calendar system.

    PHP Calendar Constants

    PHP supports many constant and you will get to work with them when you use calendar functions. Some of the calendar constants are given below-

    • CAL_GREGORIAN
    • CAL_JULIAN
    • CAL_JEWISH
    • CAL_FRENCH
    • CAL_NUM_CALS
    • CAL_DOW_DAYNO
    • CAL_DOW_SHORT
    • CAL_DOW_LONG
    • CAL_MONTH_GREGORIAN_SHORT
    • CAL_MONTH_GREGORIAN_LONG
    • CAL_MONTH_JULIAN_SHORT
    • CAL_MONTH_JULIAN_LONG
    • CAL_MONTH_JEWISH
    • CAL_MONTH_FRENCH
    • CAL_EASTER_DEFAULT
    • CAL_EASTER_ROMAN
    • CAL_EASTER_ALWAYS_GREGORIAN
    • CAL_EASTER_ALWAYS_JULIAN
    • CAL_JEWISH_ADD_ALAFIM_GERESH
    • CAL_JEWISH_ADD_ALAFIM
    • CAL_JEWISH_ADD_GERESHAYIM.

    PHP Calendar Functions

    1. cal_days_in_month()

    This function will allow checking the number of days present in the particular month of the given year of the given calendar.

    Syntax -

    cal_days_in_month ( $calendar, $month, $year );

    Example:

    <?php
       $demo_num = cal_days_in_month(CAL_GREGORIAN, 1, 20016);
       echo "There were $demo_num days in Jan 20016";
    ?>

    Output:

    There were 31 days in Jan 20016

    2. cal_from_jd()

    This function will allow you to convert the Julian day represented by jd into a calendar specified date.

    Syntax-

    cal_from_jd ( $jd, $calendar );

    Example-

    <?php
       $input = unixtojd(mktime(0, 0, 0, 8, 16, 2016));
       print_r(cal_from_jd($input, CAL_GREGORIAN));
    ?>

    Output-

    Array (
       [date] > 8/16/2016
       [month] > 8
       [day] > 16
       [year] > 2016
       [dow] > 2
       [abbrevdayname] > Tue
       [dayname] > Tuesday
       [abbrevmonth] > Aug
       [monthname] > August
    )

    3. call_to_id()

    This function will allow you to calculate the Julian day count for the specified calendar.

    Syntax-

    cal_to_jd ( $calendar, $month, $day, $year );

    Example-

    <?php
       $d = cal_to_jd(CAL_GREGORIAN,11,03,2007);
       echo $d;
    ?>

    Output- 2454408

    4. easter_date()

    This function will return the UNIX timestamp that corresponds to Easter midnight for the given year.

    Syntax-

    easter_date ( [$year] );

    Example-

    <?php
       echo date("M-d-Y", easter_date(2019));
       echo "<br />"; 
    ?>

    Output- Apr-21-2019

    5. easter_days()

    This function will return the number of the days after 21st March which coincides with Easter for the given year. If there is no year is mentioned for the function then the function will assume the current year for the calculation.

    Syntax-

    easter_days ( [$year [, $method]] );

    Example-

    <?php
       echo easter_days(1999)."\n";
       echo easter_days(1492)."\n";
    ?>

    Output-

    14
    32

    6. FrenchtoJD()

    This function will allow you to convert the french calendar date to the Julian days calendar. This function will take three parameters which represent the month, day and the year.

    Syntax-

    frenchtojd ( $month, $day, $year );

    Example-

    <?php
       $d = frenchtojd(3,3,14);
       echo($d);
    ?>

    Output- 2380650

    7. GregorianToJD()

    This function will allow you to convert a Gregorian date to the given Julian day count.

    Syntax-

    gregoriantojd ( $month, $day, $year );

    Example-

    <?php
         $jd = gregoriantojd(1, 12, 1990);
       echo "$jd";
       print "\n";
    ?>

    Output-

    2447904 

    8. JDDayOfWeek()

    This function will provide you with the day of the week. This function will take two parameters first is the Julian day which is a mandatory parameter and the second is the mode that will specify if an integer or the string value is returned.

    Syntax -

     jddayofweek ( $julianday [, $mode] );

    Example-

    <?php
       $jd = cal_to_jd(CAL_GREGORIAN,date("m"),date("d"),date("Y"));
       echo(jddayofweek($jd,1));
    ?>

    Output-

    Thursday

    9. JDMonthName()

    This function will return the month name of the provided Julian day which is the first parameter to be passed within the function. The second parameter is the mode which will allow you to specify the calendar to which you can convert the Julian day.

    Syntax-

    jdmonthname ( $julianday [, $mode] );

    Example-

    <?php
       $jd = cal_to_jd(CAL_GREGORIAN,date("m"),date("d"),date("Y"));
       echo(jdmonthname($jd,1));
    ?>

    Output-

    January

    10. JDToFrench()

    Unlike FrenchToJD(), this function will do the opposite of that function. JDToFrench() function will allow you to convert the given Julian day to the French calendar.

    Syntax-

    jdtofrench ( $juliandaycount );

    Example-

    <?php
       $d = jdtofrench(2380650);
       echo($d);
    ?>

    Output-

    3/3/14

    11. JDToGregorian()

    This function will return a string value that is the result of the conversion of the Julian day count to gregorian date and in a specific format month/day/year.

    Syntax-

    jdtogregorian ( $julianday );

    Example-

    <?php
         $gregorian = jdtogregorian($jd);
       echo "$gregorian";
    ?>

    Output-

    1/12/1990

    12. JDToJewish()

    This function will allow you to convert a Julian day count to a Jewish calendar date value. There is a second parameter present that is the Hebrew if that parameter is set to true then the fl parameter will set the format for the Hebrew parameter.

    Syntax-

    jdtojewish ( $juliandaycount, [$hebrew [, $fl]] );

    Example-

    <?php
       echo(jdtojewish(gregoriantojd(11,7,2005)) . "<br />");
       echo(jdtojewish(gregoriantojd(11,7,2005),true));
    ?>

    Output-

    2/5/5766
    ? ???? ?????

    13. JDToJulian()

    This function will allow you to convert the Julian day count Julian calendar date in the string format which is specified in the month/day/year format. Syntax -

    jdtojulian ( $juliandaycount );

    Example-

    <?php
    $julian = jdtojulian($jd);
       echo($julian);
    ?>

    Output-

    10/3/1987

    14. JDToUNIX()

    This function will allow you to return a UNIX timestamp that corresponds to the Julian day which is given in the Julian day and return the time in the local time not GMT.

    Syntax-

    jdtounix ( $jday );

    Example-

    <?php
       $jd = gregoriantojd(11,3,1990);
       $unix = jdtounix($jd);
       echo($unix);
    ?>

    Output-

    657590400

    15. JewishToJD()

    This function will allow you to convert a given date in the Jewish calendar to the corresponding Julian day count.

    Syntax- int jewishtojd ( $month, $day, $year );

    Example-

    <?php
       echo(jewishtojd(12,10,5060));
    ?>

    Output-

    2196091

    16. JulianToJD()

    This function will allow you to convert a Julian calendar date to its corresponding Julian day count.

    Syntax-

    juliantojd ( $month, $day, $year );

    Example-

    <?php
       $jd = juliantojd(10,3,2007);
       echo($jd . "\n");
    ?>

    Output-

    2454390

    17. UnixToJD()

    This function will allow you to return the Julian day for its corresponding Unix timestamp. If there is no timestamp passed to the function then it will consider to run the function for the current date.

    Syntax-

    unixtojd ( [$timestamp] );

    Example-

    <?php
       echo(unixtojd());
    ?>

    Output-

    2458086

    People are also reading: