How to change server timezone to local timezone in php
⧉ Category: Programing | 📅 Date: 17 Mar 2024 | अ🉀 Translation:


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..