Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Calendar gregoriantojd() Function



The PHP Calendar gregoriantojd() function is used to convert a given Gregorian calendar date into a Julian day count. This is useful when you want to compare dates or perform calculations in the Julian day format, which is a continuous count of days starting at a certain point. Valid Range for Gregorian Calendar 4714 B.C. to 9999 A.D.

Syntax

Below is the syntax of the PHP Calendar gregoriantojd() function −

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

Parameters

Below are the parameters of the gregoriantojd() function −

  • $month − It is the month as a number from 1 (for January) to 12 (for December).

  • $day − It is the day as a number from 1 to 31.

  • $year − It is the year as a number between -4714 and 9999.

Return Value

The gregoriantojd() function returns the Julian day for the specified Gregorian date expressed as an integer. Dates outside the valid range return zero.

PHP Version

First introduced in core PHP 4, the gregoriantojd() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

Here is the basic example of the PHP Calendar gregoriantojd() function to convert a Gregorian date to a Julian day count.

<?php
  // Convert the Gregorian date to a Julian day count
  $jd = gregoriantojd(3, 15, 2023);
   
  // Display the Julian day count
  echo "Julian Day: " . $jd;
?>

Output

Here is the outcome of the following code −

Julian Day: 2460019

Example 2

In this example, we will convert a date using the gregoriantojd() function for a leap year (February 29, 2020) to its Julian day count.

<?php
  // Convert the Gregorian date to a Julian day count
  $jd = gregoriantojd(2, 29, 2020);
   
  // Display the Julian day count
  echo "Julian Day for leap year: " . $jd;
?> 

Output

Following is the output of the above code −

Julian Day for leap year: 2458909

Example 3

This example converts the final day of the twentieth century to a Julian day count with the help of gregoriantojd() function.

<?php
  // Convert the Gregorian date to a Julian day count
  $julianDay = gregoriantojd(12, 31, 1999);
   
  // Display the Julian day count
  echo "Julian Day for End of the Year Date: " . $jd;
?> 

Output

This will create the below output −

Julian Day for End of the Year Date: 2451544

Example 4

In the below PHP code we will use the gregoriantojd() function and convert the Gregorian date to a Julian day count. And also convert the Julian day count back to a Gregorian date. Display both the dates to the console.

<?php
  // Convert the Gregorian date to a Julian day count
  $jd = gregoriantojd(1, 12, 2013);
   
  // Display the Julian day count
  echo "Julian day count: $jd";
  print "\n";
   
  // Convert the Julian day count back to a Gregorian date
  $gregorian = jdtogregorian($jd);
   
  // Display the Gregorian date
  echo "Gregorian date: $gregorian";
  print "\n";
?> 

Output

This will generate the below output −

Julian day count: 2456305
Gregorian date: 1/12/2013
php_function_reference.htm
Advertisements