Code has been added to clipboard!

How to Write a PHP Calendar Code Quickly and Easily

Reading time 4 min
Published Aug 8, 2017
Updated Oct 2, 2019

People live in different time zones and use different calendars. It is useful to stay aware of those inconsistencies when you're making a website or application as well. Using the PHP calendar feature, a need to switch from one to another might sometimes arise.

There is a whole bunch of designated functions and constants for calendars PHP supports. All you need to do is compile it with a --enable-calendar extension.

In this PHP calendar tutorial, you will find an extensive list of functions and predefined constants that you can use, complete with descriptions and versions of PHP in which they were first introduced.

PHP Calendar: Main Tips

  • PHP calendar code functions simplify different calendar conversion.
  • They are based on the Julian Day Count, starting at January 1st, 4713 B.C.
  • To convert to any other calendar precisely, convert to Julian Day Count, then to the calendar format of your choice.
  • Keep in mind that Julian Day Count and Julian Calendar are two different things!

Functions to Apply

The following table includes the majority of functions that can be applied to the PHP calendar:

Function Description
cal_days_in_month() Return number of days in month for specified year and calendar
cal_from_jd() Convert Julian Day Count into date of specified calendar
cal_info() Return information about specified calendar PHP is using
cal_to_jd() Convert date in specified calendar to Julian Day Count format
easter_date() Return Unix timestamp for midnight on Easter of specified year
easter_days() Return number of days between March 21st and the Easter Day in a specified year
frenchtojd() Convert French Republican date to Julian Day Count format
gregoriantojd() Convert Gregorian date to Julian Day Count format
jddayofweek() Return day of week
jdmonthname() Return month name
jdtofrench() Convert Julian Day Count to French Republican date
jdtogregorian() Convert Julian Day Count to Gregorian date
jdtojewish() Convert Julian Day Count to Jewish date
jdtojulian() Convert Julian Day Count to Julian date
jdtounix() Convert Julian Day Count to Unix timestamp
jewishtojd() Convert Jewish date to Julian Day Count format
juliantojd() Convert Julian date to Julian Day Count format
unixtojd() Convert Unix timestamp to Julian Day Count format

Predefined Constants

PHP constants have a lot in common with variables. The main differences are that predefined constants cannot be modified or redefined. For more information on this topic, visit this tutorial.

Constant Type PHP Version
CAL_GREGORIAN Int PHP 4 and newer
CAL_JULIAN Int PHP 4 and newer
CAL_JEWISH Int PHP 4 and newer
CAL_FRENCH Int PHP 4 and newer
CAL_NUM_CALS Int PHP 4 and newer
CAL_DOW_DAYNO Int PHP 4 and newer
CAL_DOW_SHORT Int PHP 4 and newer
CAL_DOW_LONG Int PHP 4 and newer
CAL_MONTH_GREGORIAN_SHORT Int PHP 4 and newer
CAL_MONTH_GREGORIAN_LONG Int PHP 4 and newer
CAL_MONTH_JULIAN_SHORT Int PHP 4 and newer
CAL_MONTH_JULIAN_LONG Int PHP 4 and newer
CAL_MONTH_JEWISH Int PHP 4 and newer
CAL_MONTH_FRENCH Int PHP 4 and newer
CAL_EASTER_DEFAULT Int PHP 4.3 and newer
CAL_EASTER_ROMAN Int PHP 4.3 and newer
CAL_EASTER_ALWAYS_GREGORIAN Int PHP 4.3 and newer
CAL_EASTER_ALWAYS_JULIAN Int PHP 4.3 and newer
CAL_JEWISH_ADD_ALAFIM_GERESH Int PHP 5.0 and newer
CAL_JEWISH_ADD_ALAFIM Int PHP 5.0 and newer
CAL_JEWISH_ADD_GERESHAYIM Int PHP 5.0 and newer

PHP Calendar: Summary

  • Calendar PHP functions are used to easily convert between different calendars.
  • When converting from one calendar to another, you must first convert to the Julian Day Count and only then choose the format you prefer to use from now on. Julian Day Count starts on January 1st, 4713 B.C. and is a completely different thing from the Julian Calendar - don't mix those up!
  • PHP has to be compiled with --enable-calendar option to support calendar functions.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()