This question already has answers here:
How can I get the user's local time instead of the server's time?
(6 answers)
Closed 4 years ago.
I am trying to get the time of the user and have been experimenting with timestamp but found it to display inaccurate time unless I set the timezone to that particular state but this method seems tedious for setting all the states in the world.. I have seen some website where they allow the user to select their GMT timezone.... how is this done in php?
var now = new Date();
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
The JavaScript Date object supports a number of UTC (universal)
methods, as well as local time methods. UTC, also known as Greenwich
Mean Time (GMT), refers to the time as set by the World Time Standard.
The local time is the time known to the computer where JavaScript is
executed. Invoking JavaScript Date as a function (i.e., without the
new operator) will return a string representing the current date and
time.
Related
This question already has answers here:
How to store a datetime in MySQL with timezone info
(7 answers)
Best practices with saving datetime & timezone info in database when data is dependant on datetime
(2 answers)
Closed 4 years ago.
I have read a lot of articles about best way to store user's datetime. Here is a good one. The article suggests to store datetime in two seperate column one is for UTC datetime and another is for local datetime and another column for the local timezone. If i understood correctly the local date time referred to the user's/client's date time and timezone. Now i know i can save the UTC date time using UTC_TIMESTAMP(). But how do i save the local datetime and timezone using php? I need a direction here.
Maybe this would be helpful for you
Possible duplicate get local user time instead of server
Moreover, you can fetch local date time using javascript ajax call to server.
This question already has an answer here:
Time difference in PHP using timezone function?
(1 answer)
Closed 5 years ago.
I am building a site that will be accessed in different countries. How do I make the time specific for each country timezone. For example Nigeria is GMT+1 while Ghana is GMT. How do I show time to be 6pm in Nigeria while 5pm for someone accessing the website from Ghana
You can see link:
http://php.net/manual/en/timezones.php
and to use in you php code, you will use :
date_default_timezone_set('Europe/Kiev');
or any country you need .
There are several steps in this process:
Use GMT, otherwise know at UTC, in your code and database.
Detect the country of the visitor, either by IP, or user data.
When outputting a time you convert UTC to the local time of the country of the visitor (see the answer of Ghaly Saqqal).
If you have difficulty, with any of these specific steps, you can ask a question about that.
The most popular (==standard?) way of determining the time zone I've seen around is simply asking the users themselves. If your website requires subscription, this could be saved in the users' profile data. For anon users, the dates could be displayed as UTC or GMT or some such.or
new Date().getTimezoneOffset()/60;
getTimezoneOffset() will subtract your time from GMT and return the number of minutes. So if you live in GMT-8, it will return 480. To put this into hours, divide by 60. Also, notice that the sign is the opposite of what you need -- it's calculating GMT's offset from your time zone, not your time zone's offset from GMT. To fix this, simply multiply by -1.
then use some ajax to set time zone of visitor
This question already has answers here:
How can I get the user's local time instead of the server's time?
(6 answers)
Closed 8 years ago.
I'm trying to get the date time based on user's timezone.I have tried with
echo date('Y-m-d H:i:s');
It is displaying correct date but the time is incorrect.May be it is taking server's timezone by default.Can anyone suggest me the better way to do it.
PHP is a server side language, so basically yo dont have any function that do it. you have to make the user send his time or timezone to you. for example, you can do it with ajax like here, javascript is a client side language that runs on the user computer and gets his time, then sends it to the server.
another way is to get the location of the user by his IP, there are many libraries that do it, for example this one, and create the date from his location
you can set default timezone and get time also what you want. suppose you want riyadh timezone then you can get by this code. you can put other timezone also what you want .here is the code
date_default_timezone_set('Asia/Riyadh');
here is list of timezome
http://php.net/manual/en/timezones.php
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Datatype/structure to store timezones in MySQL
How to store users timezone in mysql?
I see two options -- an offset from UTC or GMT, or storing the time zone in a string like "Asia/Seoul". I get that the offset method doesn't account for time changes, so I won't use that, but for some reason it seemed a bit odd to store a user's time zone just typed out in English. Is there some other time zone format that I overlooked?
If you're going to use PHP then a timezone string like Asia/Seoul will be easier to work with especially when you're using the DateTime suite of classes (especially DateTimeZone).
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Does PHP time() return a GMT/UTC Timestamp?
What's the best way to manage dates across PHP, MySQL, etc?
I want to store timestamps and then format them according to say, the user's timezone preference.
But I don't fully understand what the number returned by time() is.
Does it depend on the machine's timezone config?
Should I standarize it to UTC+0 before storing it?
If I use the same unix timestamp in different machines (different time zones) and format it with date(), will I have the same time (I don't mean the same format, but the same point in time)