Kamrul Khan
Kamrul Khan

Reputation: 3350

PHP : get timezone where time is as specified

I have a time range of one hour like 2015-11-23 15:00:00 to 2015-11-23 16:00:00. I want to know in which timezone the current time is within this time range.

So, if I could get it through a SQL query (just to make you understand), I would write something like

select timezone from all_timezones where time < '2015-11-23 15:00:00' and time and time < '2015-11-23 15:00:00'

What will be the best way to do it in PHP ?

Upvotes: 0

Views: 71

Answers (3)

Kamrul Khan
Kamrul Khan

Reputation: 3350

Thanks for your answers guys. Basically this is what I wanted:

function time_exists($from_date, $to_date)
{
    $lowlimit  = (strtotime($from_date) + date('Z') - time())/3600;
    $highlimit = (strtotime($to_date) + date('Z') - time())/3600;

    // $to_date must be higher than $from_date
    // $lowlimit has to be higher than -8 hours
    // $highlimit has to be lower than +14 hours

    if($lowlimit > -11 && $highlimit < 14){
       return true;
    }        
    return false;       
}

The $lowlimit and $highlimit variable also represent the timezone in GMT.

Upvotes: 0

tale852150
tale852150

Reputation: 1628

This will give you the current time zone for all date/times in your script:

date_default_timezone_get();

Example code:

<?php
echo date_default_timezone_get();
?>

You may also find the following useful:

DateTimeZone date_timezone_get ( DateTimeInterface $object )

NOTE: This example below shows how setting the timezone will give you the date/time based on the timezone set and is in response to a request for clarification from a comment:

<?php
date_default_timezone_set('America/Denver');
echo date(DATE_RFC2822);
?>

HTH

Upvotes: 1

user4431269
user4431269

Reputation:

First of all your script has a timezone setted by default you can find it like this.

echo date_default_timezone_get() . ' => ' . date('e') . ' => ' . date('T');

or just:

echo date_default_timezone_get();

You can work with differend timezones with DateTime object more info you can find in the manual.

*Edit: * You have to work with dateTime object to reach that..

Little example to get all timezone and their offset:

$timezone_identifiers = DateTimeZone::listIdentifiers();


foreach($timezone_identifiers as $tz) {
    $tz = new DateTimeZone("$tz");
    $transitions = $tz->getTransitions();
    print_r(array_slice($transitions, 0, 3));
}

if you know what 2015-11-23 15:00:00 kind of time zone is (default utc (i think)) you can easily calculate.

Hope it helps.

Upvotes: 1

Related Questions