PHP - I search for a function that if I send a Local Date and Time and the TimeZone String ==> retrieve the Julianday Number

Posted on 2016-09-20
Last Modified: 2016-11-05
PHP - I search for a function that if I send a Local Date and Time and the TimeZone String ==> retrieve the Julianday Number

Any know???

Thanks in advance... Good day at all...
Question by:astrix2
  • 3

Expert Comment

by:Brian Tao
ID: 41808119
There are PHP functions, UnixToJD or GregorianToJD, doing something similar to what you want. You can use it to write the function you want, something like the following:
// assumption:
// $localDate in the format "2016-09-21"
// $localTime in the format "14:17:32"
// $localTimezone is a valid PHP-supported timezone
function dayTimeToJulianDayNum($localDate, $localTime, $localTimezone)
  $phpTime = strtotime($localDate . " " . $localTime);
  return UnixToJD(date("U", $phpTime));

Open in new window


Author Comment

ID: 41808456
Ok thanks Brian, but not are a problem that the Unix Dates are only from 01-01-1970???

I need from 01-01-1900...

Or is valid dates prev of 01-01-1970???

Well, also I want a Script in PHP that advise of possible errors, for example, in Europe, in the last october sunday, the timezone change, when is 03 Am pass to be 2 Am, because this is posssible 2 date times, the date of the last sunday and hour 02:30 before the change, and the same after the change...

For example any person birth in the next change in Central Europe the 30 October 2016 - is Sunday - at 02:30 am, this day, at 03:00 Am pass to be 02:00 Am, but the person birth before the change of timezone is make, or after the change of timezone is make??? Not is possible know in te script, but the result of the script if possible do the 2 possible JulianDays...

Another error are when the change is the other, the Last Sunday of April in Central Europe the Timezone change, at 02:00 am, pass to be 03:00 am, and the time of day of change not possible to be 02:30 am, because not exists... The function need to do Error, not Possible this DateTime...

I search for make a deep script that solution retrieve the REAL Astronomical JulianDay from a DateTime and Timezone String, doing the possible errors, and the Date and Time from 01-01-1900 00:00 Am UTC or GMT...

Thanks in advance...
LVL 108

Accepted Solution

Ray Paseur earned 500 total points
ID: 41828742
PHP's implementation of Unix dates now cover a much larger time period (for some functions).  Check the man pages for details and supported periods.
<?php // demo/temp_astrix2.php
ini_set('display_errors', TRUE);
ini_set('log_errors',     TRUE);

function getJDate($date, $zone)
    try {
        $zone_obj = new DateTimeZone($zone);
        $date_obj = new DateTime($date, $zone_obj);
    } catch(Exception $exc) {
        return 'Error';

    $m = $date_obj->format('m');
    $d = $date_obj->format('d');
    $y = $date_obj->format('Y');
    $jd = GregorianToJD($m, $d, $y);

    return $jd;

$dates = array
( 'March 24, 1917'
, 'March 24, 1917 12:35pm'
, 'January 1, 1970'
, 'Yesterday'
, 'Today'
, 'Foo'

$zone = 'America/Chicago';

foreach ($dates as $date)
    echo PHP_EOL . '<br>';
    echo $date;
    echo ' = JULIAN: ';
    echo getjDate($date, $zone);

Open in new window

Outputs something like:
March 24, 1917 = JULIAN: 2421312 
March 24, 1917 12:35pm = JULIAN: 2421312 
January 1, 1970 = JULIAN: 2440588 
Yesterday = JULIAN: 2457665 
Today = JULIAN: 2457666 
Foo = JULIAN: Error

Open in new window

See also:

Author Comment

ID: 41828945
Thanks Ray, but the problem is that JulianDay not do the decimals, the time of the day...

Also PHP timezones DST changes are bad, not are equivalent to IANA Olson Timezones Databases:

PHP timezones functions not coincide exactly with the hours of change, for example, in Central Europe the Last Sunday of March at 02:00 Am pass to be 03:00 Am, and also, the Last Sunday of October at 03:00 Am pass to be 02:00 am, but PHP Timezones functions do that the changes is making at 01:00 Am in the two dates of DST changes, for example this:
$zonen = $_GET['zonename'];
$timezone = new DateTimeZone($zonen);
$transitions = $timezone->getTransitions();
for ($i=0; $i<500; $i++)
	if ($transitions[$i] == "")
	print '<br>';
$timezone_identifiers = DateTimeZone::listIdentifiers();
for ($i=0; $i < 1005; $i++)
	if ($timezone_identifiers[$i] == "")
	echo "$timezone_identifiers[$i]";
	print '<br>';

Open in new window

And see how, for example, 2016:

Array ( [ts] => 1459040400 [time] => 2016-03-27T01:00:00+0000 [offset] => 7200 [isdst] => 1 [abbr] => CEST )

Array ( [ts] => 1477789200 [time] => 2016-10-30T01:00:00+0000 [offset] => 3600 [isdst] => [abbr] => CET )

And NOT, the DST changes in cities of Central Europe they are made at 02:00AM in March and at 03:00AM in October... Are not reliable the PHP Timezones Functions...

Also the JulianDay conversions not have the possibility of do directly the decimals, the hours, minutes and seconds in decimal format, only rounds to the more near JulianDay Long Integer...

But thanks Ray for your message, very thanks...

I give this information for if someone wants to use Timezones PHP functions, be careful, are nothing reliable in its changes ...

Inclusive I found sites that offer the service to provide timezones data, and they say they are fully equivalent to the MORE SECURE reference Olson IANA Database, and it is false, have PHP data, and PHP Tz Functions not are good...

Good day at all...

Author Comment

ID: 41829106
Uhmm, not, I have an error, PHP timezones do Good the DST changes...

Pondering, I discover my error, when PHP Timezones Functions do this:

Array ( [ts] => 1459040400 [time] => 2016-03-27T01:00:00+0000 [offset] => 7200 [isdst] => 1 [abbr] => CEST )

Array ( [ts] => 1477789200 [time] => 2016-10-30T01:00:00+0000 [offset] => 3600 [isdst] => [abbr] => CET )


This time of change are in UTC or GMT... And yes, is good time of the changes, but is in UTC or GMT...

Ok, sorry for the error... Php Timezones Functions are GOOD... Are equal to IANA Olson timezones Databases...

Good day at all...

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now