Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 72
  • Last Modified:

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

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...
0
astrix2
Asked:
astrix2
  • 3
1 Solution
 
Brian TaoSenior Business Solutions ConsultantCommented:
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)
{
  date_default_timezone_set($localTimezone);
  $phpTime = strtotime($localDate . " " . $localTime);
  return UnixToJD(date("U", $phpTime));
}

Open in new window

0
 
astrix2Author Commented:
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...
0
 
Ray PaseurCommented:
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.
https://iconoun.com/demo/temp_astrix2.php
<?php // demo/temp_astrix2.php
/**
 * https://www.experts-exchange.com/questions/28971238/PHP-I-search-for-a-function-that-if-I-send-a-Local-Date-and-Time-and-the-TimeZone-String-retrieve-the-Julianday-Number.html
 *
 * https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
 *
 * http://php.net/manual/en/function.gregoriantojd.php
 */
error_reporting(E_ALL);
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) {
        error_log($exc->getMessage());
        return 'Error';
    }

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

    return $jd;
}

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

// TEST TIME ZONE
$zone = 'America/Chicago';

// RUN THE TESTS AND VISUALIZE THE OUTPUTS
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:
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
0
 
astrix2Author Commented:
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:

https://www.iana.org/time-zones

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:

http://www.astrodreams.com/testphp/gettzwithtzname.php?zonename=Europe/Madrid
<?php
$zonen = $_GET['zonename'];
$timezone = new DateTimeZone($zonen);
$transitions = $timezone->getTransitions();
for ($i=0; $i<500; $i++)
	{
	if ($transitions[$i] == "")
		break;
	print_r($transitions[$i]);
	print '<br>';
	}
$timezone_identifiers = DateTimeZone::listIdentifiers();
for ($i=0; $i < 1005; $i++)
	{
	if ($timezone_identifiers[$i] == "")
		break;
	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...
0
 
astrix2Author Commented:
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 )
/////////////////////////////////////////////

2016-03-27T01:00:00

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...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now