I have my project based in India and my hosting server located somewhere that is at a difference of -5:30 HRs from Indian time.
So my time being stored in database is coming as based on there timings. How can I rectify this in PHP so that the time actually gets stored on India.
I tried using date_default_timezone_set('Asia/Kolkata');
but still the time gets stored with a difference of -30mins to -45mins. What could be possible code fort his so that the time sets exactly according to Indian timings. Any advice will be very helpful.
Step-1: Set timezone as asia/kolkata
Step-2: Now, first store the
date in local variable, example: $mydate = date('Y-m-d H:i:s'); and
just print the variable value to see, if it is the time as you want?
Step-3: If yes then store $mydate variable value in database.
You can set the timezone in the database.
Few more threads of the same topic are here:
MySQL Time Zones
Set MySQL database timezone to GMT
MySQL timezone change?
If you want to store the current date/time on your database and use the timezone you have. Try to use now() function.
Reference Link: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_now
As you said your server is running at -5.30 hours from India, get the dates like this:
date('d-m-Y H:i:s',strtotime('+330 minutes')); // this adds 5.30 hours to the current time.
Related
I have a created a timestamp in MySQL that changes when an account is updated by a users, and this timestamp is echoed on the page. However it is displaying the server time rather than my local time. I can't set the timezone in MySQL, I tried. What is another way to change this, and how can it be implemented?
The problem with timestamp datatype is that
MySQL converts TIMESTAMP values from the current time zone to UTC for
storage, and back from UTC to the current time zone for retrieval.
(This does not occur for other types such as DATETIME.) By default,
the current time zone for each connection is the server's time. The
time zone can be set on a per-connection basis. As long as the time
zone setting remains constant, you get back the same value you store.
If you store a TIMESTAMP value, and then change the time zone and
retrieve the value, the retrieved value is different from the value
you stored.
So, it does not really matter what your timezone setting in php is, you need to set either mysql's timezone on a session basis using
SET time_zone = timezone
command, or you need to store the timezone of the client along with the timestamp and adjus the value based on the stored timezone. I would use the latter approach, since a client technically can change timezones and if the client access the timestamp data from a different timezone, then different data will be returned by mysql.
When echoing from a database add in some "filters" seemed to do the trick
<?php echo $row['field name']; strtotime(date("Y-m-d", 1310571061)); ?>
Without the above code it displayed the timestamp as: 2016-02-09 00:00:00
With the above code it displays thus: Tuesday, February 09, 2016
I'm working on some website (news portal) in PHP and MySQL. I have a rubric last post with time when news are posted. My timezone and server timezone is GMT+2. I want to display a time correctly to all clients from all time zones. Example, if news is posted at 13:05 at my timezone to display for clients from GMT+3 14:05, for clients from GMT+4 zone 15:05, for GMT+1 12:05, and so on. How to do that? What is solution and what is a best way to store date and time in MySQL database for that purpose? I'm also integrate Carbon in my website but I can't find method for that.
What we do in our company is we store all times in GMT. Even though we are in South-Africa. Then based on the user's location we convert the time to display correctly. There are a number of ways to determine where a user is located and convert the time, however the easiest method is to just ask them where they live and store that information.
Keep dates in timestamp.
Store timezones for all clients.
After establishing connection to database run query like that:
SET ##session.time_zone = "+02:00";
or
SET ##session.time_zone = "Europe/Warsaw";
Where timezone offset or name is value stored for client.
All timestamp values will be returned from queries in defined timezone.
While storing values in db store in UTC timezone something like below
date_default_timezone_set('UTC');
Then while displaying the values get the user timezone using any timezone js and send the timezone to server using ajax and store it in SESSION variable and while displayin the date add that timezone.
For example
1)Download the timezone js from https://bitbucket.org/pellepim/jstimezonedetect/src
Following will give you the timezone
var tz = jstz.determine(); // Determines the time zone of the browser client
var timezone=tz.name(); // Returns the name of the time zone eg "Europe/Berlin"
Make ajax call like below
$.post("scripts/set_timezone.php", { timezone: timezone} );
and in set_timezone.php have code
session_start();
$_SESSION['user_timezone'] = $_POST['timezone'];
and while displaying display as follows
$date = new DateTime('2012-01-23 05:00:00', new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone($_SESSION['user_timezone']));//This will from the session e.g Asia/Kolkata
$time= $date->format('Y-m-d H:i:s');
I live in Denmark - but am setting up a page for a friend in USA (Washington State). The page is hosted at Surftown, in Denmark.
I know there is a 9 hour difference, so I set:
date_default_timezone_set('America/Los_Angeles');
But there is something I obviously don't quite understand about time zones / date() and strtotime() because:
Via text input I am trying to save a specific date and time to the database.
Lets say that $_POST[date] input is: '01/29/2015' and $_POST[time] input is: '02:00 PM'.
I then create a stamp using:
strtotime($_POST[date].' '.$_POST[time]);
But when I try to output this, I get the correct date - but 9 hours is added to time? Why is this?
I guess I could just remove the time zone setting for this specific task - but
I'd like to understand why. I am setting the time zone because I also need to save some timestamps based on the actual time of the user (in Washington state - not Denmark).
Can you help?
strtotime assumes you are passing a UTC date so it converts it to the server timezone, what you can do is the following:
$date = new DateTime($_POST[date].' '.$_POST[time], new DateTimeZone("UTC"));
echo $date->getTimestamp();
I believe I'm going mad.
$expire = date('Y-m-d H:i:s', strtotime("+30 minutes") );
It works as expected when echoed, yet when I insert it into a custom table via the wordpress database class, into a datetime column it's showing current time minus 30 mins.
Am I mad?
Simply given the time frame you are working with, it makes me wonder if you are seeing a Standard/Daylight time conflict, assuming you observe daylight savings time where you are.
First, See if you experience the same issue with "+60 minutes" as a test. If it is then -60 in the database, then it may be a bug, however, if it is then the current time in the database, it may actually be a timezone issue.
Make sure all of your timezones are properly configured on your host OSes and in your database. If the database column is of the type "timestamp" then mysql converts it to UTC on storage, and back on retrieval, so a mis-configured timezone could cause a 1 hour offset there as well.
Hope This Helps!
All date and time functions are now dependent upon a correct timezone setting.
You can either do this in your script
date_default_timezone_set('America/Los_Angeles'); // for example
OR
Check your php.ini for this setting
date.timezone = UTC
And set it correctly for your specific timezone, here is a List of supported timezones which you will need either way
I currently use $curdate=date('Y-m-d H:i:s'); to enter a timestamp to my blog's MySQL.
The problem is that the timezone of my MySQL is 2 hours ahead. At least in the timesaving period (I don't know if it is going to be any different when the timesaving period is over).
How should I redefine $curdate so that it records correct time based on PST time?
You can set the timezone the PHP uses for the duration of the execution of your script with date_default_timezone_set().
If you need to do something in your own timezone later in the execution of you script, you can call it again to set it back.
Alternatively (better?), if you use the MySQL NOW() function in your query, the time entered into the database will be calculated by MySQL, according to it's own timezone.