Set timezone in PHP and MySQL

In PHP: <?php define(‘TIMEZONE’, ‘Europe/Paris’); date_default_timezone_set(TIMEZONE); For MySQL: <?php $now = new DateTime(); $mins = $now->getOffset() / 60; $sgn = ($mins < 0 ? -1 : 1); $mins = abs($mins); $hrs = floor($mins / 60); $mins -= $hrs * 60; $offset = sprintf(‘%+d:%02d’, $hrs*$sgn, $mins); //Your DB Connection – sample $db = new PDO(‘mysql:host=localhost;dbname=test’, ‘dbuser’, … Read more

Java Time Zone is messed up

It’s a “quirk” in the way the JVM looks up the zoneinfo file. See Bug ID 6456628. The easiest workaround is to make /etc/localtime a symlink to the correct zoneinfo file. For Pacific time, the following commands should work: # sudo cp /etc/localtime /etc/localtime.dist # sudo ln -fs /usr/share/zoneinfo/America/Los_Angeles /etc/localtime I haven’t had any problems … Read more

How to get Time Zone through IP Address in PHP [duplicate]

$ip = “189.240.194.147”; //$_SERVER[‘REMOTE_ADDR’] $ipInfo = file_get_contents(‘http://ip-api.com/json/’ . $ip); $ipInfo = json_decode($ipInfo); $timezone = $ipInfo->timezone; date_default_timezone_set($timezone); echo date_default_timezone_get(); echo date(‘Y/m/d H:i:s’); Sometime it won’t work on local server so try on server. Edit: This data is coming from ip-api.com, they’re free to use as long as you don’t exceed 45 requests per minute and not … Read more

Timezones in SQL DATE vs java.sql.Date

The JDBC specification does not define any details with regards to time zone. Nonetheless, most of us know the pains of having to deal with JDBC time zone discrepencies; just look at all the StackOverflow questions! Ultimately, the handling of time zone for date/time database types boils down to the database server, the JDBC driver … Read more

Convert UTC dates to local time in PHP

If by client, you mean browser, then you first need to send the timezone name to PHP from the browser, then do the conversion as described below. Answer Convert the UTC datetime to America/Denver // create a $dt object with the UTC timezone $dt = new DateTime(‘2016-12-12 12:12:12’, new DateTimeZone(‘UTC’)); // change the timezone of … Read more

using time zone in pandas to_datetime

You can use tz_localize to set the timezone to UTC/+0000, and then tz_convert to add the timezone you want: start = pd.to_datetime(‘2015-02-24’) rng = pd.date_range(start, periods=10) df = pd.DataFrame({‘Date’: rng, ‘a’: range(10)}) df.Date = df.Date.dt.tz_localize(‘UTC’).dt.tz_convert(‘Asia/Kolkata’) print (df) Date a 0 2015-02-24 05:30:00+05:30 0 1 2015-02-25 05:30:00+05:30 1 2 2015-02-26 05:30:00+05:30 2 3 2015-02-27 05:30:00+05:30 3 … Read more

Converting time zone pandas dataframe

Localize the index (using tz_localize) to UTC (to make the Timestamps timezone-aware) and then convert to Eastern (using tz_convert): import pytz eastern = pytz.timezone(‘US/Eastern’) df.index = df.index.tz_localize(pytz.utc).tz_convert(eastern) For example: import pandas as pd import pytz index = pd.date_range(‘20140101 21:55′, freq=’15S’, periods=5) df = pd.DataFrame(1, index=index, columns=[‘X’]) print(df) # X # 2014-01-01 21:55:00 1 # 2014-01-01 … Read more