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
Related
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.
This question already has answers here:
PHP > Set Default Timezone
(2 answers)
Closed 5 years ago.
My question is pretty simple but I'm not sure how to do it.
Currently I have a script that loads time and account and etc and executes it in the database but this is what I'm using.
$time = time();
The issue here is that it's loading the time from their own computer which can easily be abused since my script only enables you to use it every x hour(s). So what I'd like to do is to set a default timezone that the script will use instead of loading the time from their own computer.
Thank you in advance.
I'd determine the time in the database. I don't know which DB you're using but in MySQL you can use NOW() as a default value and you don't need to worry about it in your script anymore.
You can use date_default_timezone_set method to set default timezone, see example below:
date_default_timezone_set('America/Los_Angeles');
This question already has answers here:
Should I use the datetime or timestamp data type in MySQL?
(40 answers)
Closed 9 years ago.
I have searched around and haven't found a specific answer to my question..
I am wondering the best practice for storing timestamp information.
Example:
user1 logs in in Florida (-0500) at 3:00pm EST
The admin logs into the system an hour later from California (-0800) at 4:00pm
I want the admin to see the log onscreen and see that user1 logged in 1 hour ago..
My brain is going nuts bc i feel that i am over complicating this..
I should be able to use UNIX time stamp and then adjust with the -0800 or -0300.
Do i store the users timezone offset in their profile information?
Do i store the -0300 with the log entry.
Any input would be greatly appreciated!
You are mixing up client timezone and server timezone.
As long as you do not care where your users sit, you have absolutely no problem as long as you do not change the server timezone. You can set a default timezone in MySQL and in PHP and this one will be used, independent of the timezone of the visitor.
If you want to respect the timezone of your visitors, it gets slightly more complicated. You need do transform a timestamp from one timezone into another. You can find information concerning that in the PHP manual if you want to achieve this with PHP. You can read this SO thread if you are interested in how MySQL stores dates internally (difference between TIMESTAMP and DATETIME type), which opens the possibility to use some of mysql's features, and which you should definitely take into account when you implement a global project.
Timezone can be configured dynamically in the PHP, so you don't need to store time offsets in your DB. Storing unix timestamp is a good solution.
You can store timestamp as INTEGER datatype.
Then, you can use JS on client side to get the timezone of user.
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).
Well I know that this question may be asked many times but certainly I've few doubts In my mind, and by the way don't comment that what I've tried, I've tried many ways but am just asking what's the correct and easier way as there are many posts lingering out here with different suggestions for accomplishing these tasks, so I'll explode() my question into smaller questions...
So can you people just guide me where am going right or wrong as am sure many people are confused when it comes to date/time
1) Why/how to save time as UTC in MySQL using PHP?
Personally for this I post this using php $year/$month/$day and check the date using checkdate() in mysql date field. so is it ok or should I use timestamp and than explode the retrieved string on front end using php?
2) If I run server from India, should I record default time using date_default_timezone_set("Asia/Calcutta"); and than subtract and add time using php function or while posting only I should check users time zone selected from his user accound and accordingly set a condition kinda:
if(timezoneselected == +5.30) {
echo date_default_timezone_set("Asia/Calcutta");
} elseif(timezoneselected == +anytime) {
echo another country timezone
}
3) last question is how websites like gmail facebook etc manages time? I mean if they are saving datetime according to their server than how they show perfect posted time for each user, even gmail, if I send a mail to another user, my sent time and the person living in another country gets email at his printed local time I mean how we can do this, sorry am not able to explain you perfectly say this example,
facebook:
user from India posts, facebook shows posted 8 mins ago, 9mins ago, fine after sometime they show a real date, and that date is perfect according to the time I posted, however if a person from USA updates, say 8 mins ago 9mins ago on his profile but his original posted time is shown correctly to him, and even correctly to me?
sorry for this question but really this will help me understanding this date/time concept and will also be helpful to future users. Thank you!
Bottom line, you should store everything UTC
When you display times for a particular user, use a timezone of their choosing. Store the timezone of the user, like "Asia/Calcutta" and simply convert the time when displaying it using the date_default_timezone_set method.
I will attempt to answer your questions from the comment here.
You store everything UTC always. It is the baseline. When you display the times associated with anything you convert based on the user. If you want to display Posted 8 mins ago then you are taking the delta between the current UTC time and the UTC time associated with a post. If you send a message from user A (in India) to user B (in Los Angeles, USA) then you would store the message time in UTC. If user A is viewing it, the time would be converted to "Asia/Calcutta" and if user B is viewing it, the time would be converted to "America/Los_Angeles". Using UTC will make your life a lot easier. Trust me.
As described in MySQL Server Time Zone Support:
The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as NOW() or CURTIME(), and values stored in and retrieved from TIMESTAMP columns. Values for TIMESTAMP columns are converted from the current time zone to UTC for storage, and from UTC to the current time zone for retrieval.
Therefore, if you use TIMESTAMP type columns, MySQL will handle timezone conversion for you automatically: just set the appropriate timezone for the session in its time_zone variable.