How to store dates in a portable way? [duplicate] - php

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)

Related

Confused with "how to store local datetime and Timezone using php/Mysql" [duplicate]

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.

How to get the accurate timezone in php [duplicate]

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.

How to get date time based on Timezone [duplicate]

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

Timestamp Best Practices - PHP, MySQL [duplicate]

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.

What format should I use to store a user's preferred time zone in a MySQL table? [duplicate]

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

Categories