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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

730 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