PHP: Show time based on user's timezone

If you added the users Timezone while registering or you know users Timezone somehow, then you can try this...

$users = [
    [
        'username' => 'foo',
        'timezone' => 'America/Los_Angeles'
    ],
    [
        'username' => 'bar',
        'timezone' => 'America/New_York'
    ]
];

foreach ($users as $user) {
    date_default_timezone_set($user['timezone']);
    echo $user['username'] . ' date and time is ' . date('Y-m-d H:i:s') . '<br />';
}

Output

foo time is 2013-02-28 18:13:49
bar time is 2013-02-28 21:13:49

You can also user Timezone in JavaScrpt Getting the client's timezone in JavaScript. Make an Ajax request and Save in PHP Session or database.


You must get the user information and set the default timezone

Look date_default_timezone_set()

And ip geolocation api

Tags:

Php

Timezone