Viewed   70 times

I'm being told that this below method of calculating the user's local time is sometimes not working. What's the best way to do this in PHP? What do you do?

public function getTimeOffset ($time) {
    $this->_cacheTimeOffset();  
    if ($this->timeOffsetExecuted) {
        $d = date("O");
        $neg = 1;

        if (substr($d, 0, 1) == "-") {
            $neg = -1;
            $d = substr($d, 1);
        }

        $h = substr($d, 0, 2)*3600;
        $m = substr($d, 2)*60;

        return $time + ($neg * ($h + $m) * -1) + (($this->timeOffset + $this->getDstInUse()) * 3600);
    }
    return $time;
}

 Answers

1

Use the DateTime extension, such as DateTime::getOffset,
or DateTimeZone::getOffset

Some countries might have perform several timezone update,
this method DateTimeZone::getTransitions reveal the transition history

Saturday, October 1, 2022
 
abhiraz
 
2

Normally connections happen once a page load. AKA

class Database{
    public function connect()
    {
         $this->connection = mysql_connect();
    }

    // This will be called at the end of the script.
    public function __destruct()
    {
        mysql_close($this->connection);
    }

    public function function query($query)
    {
        return mysql_query($query, $this->connection);
    }
}
$database = new Database;
$database->connect();

$database->query("INSERT INTO TABLE (`Name`) VALUES('Chacha')");

Basically, you open the connection in the beginning of the page, close it at the end page. Then, you can make various queries during the page and don't have to do anything to the connection.

You could even do the mysql_connect in the constructor as Erik suggest.


To use the above using global variables (not suggested as it creates global state), you would do something like

Global $db;

$db = new Database;
// ... do startup stuff

function doSomething()
{
    Global $db;
    $db->query("Do Something");
}

Oh, and no one mentioned you don't have to pass around a parameter. Just connect

mysql_connect();

Then, mysql_query will just use the last connection no matter what the scope is.

mysql_connect();

function doSomething()
{
    mysql_query("Do something");
}

Per the comments:

I think you should use mysql_pconnect() instead of mysql_connect(), because mysql_connect() doesn't use connection pooling. – nightcoder

You might want to consider whether you use mysql_connect or mysql_pconnect. However, you should still only connect once per script.

Tuesday, December 13, 2022
 
exsnake
 
3

The refer(r)er can be found in the $_SERVER global variable. Please note that sending the refer(r)er is completely optional and a user-agent can simply decide to not send the refer(r)ing url.

$_SERVER['HTTP_REFERER']
Wednesday, August 3, 2022
 
5

According to the php.ini directives manual page, the timezone isn't set by default if you're using a version prior to 5.3. Then if you take a look at the page for date_default_timezone_get, it uses the following order for getting the timezone:

* Reading the timezone set using the date_default_timezone_set() function (if any)
* Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
* Reading the value of the date.timezone ini option (if set)
* Querying the host operating system (if supported and allowed by the OS)

UTC is the default if all of those fail, so that's probably a good starting point.

Friday, August 19, 2022
 
2
$src_tz = new DateTimeZone('America/Chicago');
$dest_tz = new DateTimeZone('America/New_York');

$dt = new DateTime("2000-01-01 12:00:00", $src_tz);
$dt->setTimeZone($dest_tz);

echo $dt->format('Y-m-d H:i:s');

Note that if the source time is UTC, you can change the one line to this:

$dt = new DateTime("2000-01-01 12:00:00 UTC");

Edit: Looks like you want to go to UTC. In that case, just use "UTC" as the parameter to the $dest_tz constructor, and use the original block of code. (And of course, you can omit the $src_tz parameter if it is the same as the default time zone.)

Tuesday, December 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :