How to change server timezone to local timezone in php

⧉ Category: Programing | 📅 Date: 17 Mar 2024 | अ🉀 Translation:
How to change server timezone to local timezone in php Using the default time zone suggested by the server is not very good, especially for users. It can be a bit confusing when the users are from different time zones. Here's an example: I have a 'Contact Us' page on this website where users can send messages about their queries and problems. When a user sends a message, the default time zone leads me to believe the user sent the message yesterday, even though the message was sent 5 hours ago. Although this is not a big problem for me right now, I searched online for a possible solution and found a simple PHP function that can make this problem disappear. date_default_timezone_set() How to use it? This is more useful when your website has user preference settings. While each user chooses their own custom website settings. Currently this website doesn't have member portal so in my case I use it only in admin panel. If you plan to allow users to set their own time zone, you can use session, cookies, or IP to lock the time zone and set it to the local time zone. How to change server timezone to local timezone in php Example: Here's an example of using the date_default_timezone_set() to create a dropdown list of countries for the user to select their preferred time zone. The given HTML form contains few countries and their time zones.
Select Your country <form name='select_country' action="/?page=test" method="post"> <select name="countries" onchange="submit()"> <option value='Asia/Kolkata' >India</option> <option value='Europe/Paris'>France</option> <option value='Asia/Shanghai' >China</option> <option value='America/New_York' >USA</option> <option value='Europe/London' >UK</option> <option value='Asia/Qatar' >Qatar</option> <option value='Asia/Tokyo' >Japan</option> <option value='Australia/Melbourne' >Australia</option> </select> </form>
From the posted data, find the country selected by the user and put it in the date_default_timezone_set() function. You can use the date_default_timezone_get() function to see the current timezone selection.
$countries = ["Asia/Kolkata" => "India", "Europe/Paris" => "France", "Asia/Shanghai" => "China", "America/New_York" => "USA", "Europe/London" => "UK", "Asia/Qatar" => "Qatar", "Asia/Tokyo" => "Japan", "Australia/Melbourne" => "Australia"]; if(filter_input(INPUT_POST, "countries")) { $get_country = filter_input(INPUT_POST, "countries"); date_default_timezone_set($get_country); $country = date_default_timezone_get(); echo "You have chosen '{$countries[$country]}' as your country"; }
You may set a cookie to remember user selections and use them for future reference.
$expiry = time()+(60*60*24*30); setcookie("timezone", $get_country, $expiry, "/"); if(filter_input(INPUT_COOKIE, "timezone")) { $get_cookie = filter_input(INPUT_COOKIE, "timezone"); }
Using the array we already created above to create the dropdown list will make it easy to add more items to the list. This will help remember the user's selection without using JavaScript.
$countries_list = ""; foreach ($countries as $key => $value) { $selected = ""; if(isset($get_cookie) && $get_cookie === $key) { $selected = "selected='selected' "; } $countries_list .= "<option value='{$key}' {$selected}>{$value}</option>"; }
For the full list of countries Timezones, please refer php's official website : https://www.php.net/manual/en/timezones.php Here is the full code
$countries = ["Asia/Kolkata" => "India", "Europe/Paris" => "France", "Asia/Shanghai" => "China", "America/New_York" => "USA", "Europe/London" => "UK", "Asia/Qatar" => "Qatar", "Asia/Tokyo" => "Japan", "Australia/Melbourne" => "Australia"]; if(filter_input(INPUT_POST, "countries")) { $get_country = filter_input(INPUT_POST, "countries"); date_default_timezone_set($get_country); $country = date_default_timezone_get(); $expiry = time()+(60*60*24*30); setcookie("timezone", $get_country, $expiry, "/"); echo "You have chosen '{$countries[$country]}' as your country"; } if(filter_input(INPUT_COOKIE, "timezone")) { $get_cookie = filter_input(INPUT_COOKIE, "timezone"); } $countries_list = ""; foreach ($countries as $key => $value) { $selected = ""; if(isset($get_cookie) && $get_cookie === $key) { $selected = "selected='selected' "; } $countries_list .= "<option value='{$key}' {$selected}>{$value}</option>"; } /* HTML section */ Select Your country <form name='select_country' action="/?page=test" method="post"> <select name="countries" onchange="submit()"> <?php echo $countries_list; ?> </select> </form>
«[Previous Post] How to get next and previous rows in database usin..Create an Analog Clock using Javascript [Next Post]»

Comments

Please solve above captcha based on the hint given..
f