help me php script auto detect timezone at any webserver!

Skilled Illusionist
Joined
Apr 17, 2010
Messages
311
Reaction score
22
I have write a simple php script that auto detect the timezone, i use this to detect the real time at any email host server. but there was a bug i need an expert to fixed it! i use this code for almost 2 months! but i want to fixed the other error!! here is a code!
PHP:
<?php

        $date_time0 = "Wednesday, July 14, 2010 11:30 AM"; // this is a time to the email host but i dont know the timezone of their server because every email host have different timezone.

        $date_time0 = date("H", $time0);

        $time00 = date("l F d Y h:i:s A");

        $dateTime = new DateTime($time00, new DateTimeZone(date_default_timezone_get())); // this is a timezone of my server

        $dateTimeZone = new DateTimeZone('GMT'); // then convert it to GMT time +0

        $dateTime->setTimezone($dateTimeZone);

        $time00 = $dateTime->format("H");

        $offset0 = $date_time0 - $time00; // then subtract the unknown timezone to GMT+0 timezone to get the off set!
         
    
        switch($offset0){
        case -12:
        $timezone0 = "Etc/GMT+12";
        break;
        case -11:
        $timezone0 = "Etc/GMT+11";
        break;
        case -10:
        $timezone0 = "Etc/GMT+10";
        break;
        case -9:
        $timezone0 = "Etc/GMT+9";
        break;
        case -8:
        $timezone0 = "Etc/GMT+8";
        break;
        case -7:
        $timezone0 = "Etc/GMT+7";
        break;
        case -6:
        $timezone0 = "Etc/GMT+6";
        break;
        case -5:
        $timezone0 = "Etc/GMT+5";
        break;
        case -4:
        $timezone0 = "Etc/GMT+4";
        break;
        case -3:
        $timezone0 = "Etc/GMT+3";
        break;
        case -2:
        $timezone0 = "Etc/GMT+2";
        break;
        case -1:
        $timezone0 = "Etc/GMT+1";
        break;
        case 0:
        $timezone0 = "GMT";
        break;
        case 1:
        $timezone0 = "Etc/GMT-1";
        break;
        case 2:
        $timezone0 = "Etc/GMT-2";
        break;
        case 3:
        $timezone0 = "Etc/GMT-3";
        break;
        case 4:
        $timezone0 = "Etc/GMT-4";
        break;
        case 5:
        $timezone0 = "Etc/GMT-5";
        break;
        case 6:
        $timezone0 = "Etc/GMT-6";
        break;
        case 7:
        $timezone0 = "Etc/GMT-7";
        break;
        case 8:
        $timezone0 = "Etc/GMT-8";
        break;
        case 9:
        $timezone0 = "Etc/GMT-9";
        break;
        case 10:
        $timezone0 = "Etc/GMT-10";
        break;
        case 11:
        $timezone0 = "Etc/GMT-11";
        break;
        case 12:
        $timezone0 = "Etc/GMT-12";
        break;
        case 13:
        $timezone0 = "Etc/GMT-13";
        break;
        case 14:
        $timezone0 = "Etc/GMT-14";
        break;
        }

$date_time0 = date("l F d Y h:i:s A", $date_time0);

$dateTime = new DateTime($time0, new DateTimeZone($timezone0)); // and this is a timezone that we can get to switch() function

$dateTimeZone = new DateTimeZone('Asia/Manila'); //then the time was convert it to my country

$dateTime->setTimezone($dateTimeZone);

$date_time0 = $dateTime->format("l F d Y h:i:s A");

echo $date_time0; // and this is the real time to my country.
    
?>


I hope you an idea to get it fixed and more stable i think the problem is in the getting the offset!!

Guys Help me please!!

Thanks In advance!!
 
Last edited:
If you're using WebDav or XAMPP as the web server, just complete the install and then when you use the date() php function, it will auto detect the timezone. But if you don't install it correctly, it won't work.
 
I don't think you've declared $time0 + this ^
Unless you want the time of the server being displayed...
 
Last edited:
waldopulanco said:
No you dont understand!
No.. We dont understand, please explain your case... and use proper english.
Why dont you just put a time() +GMT >> into mysql
You retrieve it and do the hand work... it wont hurt that much
 
Last edited:
No.. We dont understand, please explain your case... and use proper english.
Why dont you just put a time() +GMT >> into mysql
You retrieve it and do the hand work... it wont hurt that much

sorry sir!!

I think you are right, i store the time() to mysql then i use it to all email!!

Thank you sir.
and also thanks to all who post and try to help me.

Best Wishes
Waldo Pulanco
 
Back