如何将MySQL Datetime UMT转换为实际时区

时间:2018-12-03 18:10:19

标签: mysql datetime timezone-offset

我有一个数据库,它以UTC格式存储来自GPS跟踪器的日期时间。 例如:

CREATE TABLE IF NOT EXISTS `tab_data` (
  `id` int(6) unsigned NOT NULL,
  `utc_time` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `tab_data` (`id`, `utc_time`) VALUES
  ('1', '2018-01-05 07:00:00'), //this should be in UTC +1
  ('2', '2018-02-04 01:10:00'), //this should be in UTC +1
  ('3', '2018-07-01 15:00:00'), //this should be in UTC +2
  ('4', '2018-08-22 16:00:00'); //this should be in UTC +2

我想要做的是在适当的时候从波兰获取此数据。 像欧洲许多国家一样,在波兰,我们有夏季和冬季: -冬季是CET时区(UTC + 1) -夏天是CEST时区(UTC + 2)

如何将数据库中的此DateTime转换为正确的时间(+1或+ 2h)

通常我将使用此查询,但是此时间必须转换为一年中适当时间适用的时区

SELECT `utc_time` AS time FROM `tab_data`;

我知道这是选项CONVERT_TZ,但是据我所知,我只能将其从一个时区转换为另一个时区,而不能从两个不同的时区转换。

这里是SQL提琴 http://sqlfiddle.com/#!9/a09700/2

如果这在MySQL中很难做到,我也可以在PHP中实现

2 个答案:

答案 0 :(得分:2)

此代码对您有用吗?

  

选择<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Font Awesome Icon Library --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <!---J-Query ---> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!--- Animation Ajax ---> <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/2.0.2/anime.min.js"></script> <!---- NUMARUL 10 -----> <div class="container-fluid"> <div class="col-sm-6 col-md-offset-3"> <h2 id="primul" style="color:darkmagenta;"> 10. Samsung S9+ </h2> <div class="thumbnail"> <img src="telefoane/s9.jpg" alt="Samsung S9+"> <!-- Thin Size Progress Bars --> <div class="margin"> <h3 class="progress-label">Rapiditate <span class="pull-right">85%</span> </h3> <div class="progress progress-xxs" style="height:12px;"> <div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="92" aria-valuemin="0" id="first" aria-valuemax="100" style="width: 92%"> </div> </div> <h3 class="progress-label">Rezistenta <span class="pull-right">68%</span> </h3> <div class="progress progress-xxs" style="height:12px;"> <div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="68" aria-valuemin="0" id="second" aria-valuemax="100" style="width: 68%;"> </div> </div> <h3 class="progress-label">Performanta <span class="pull-right">81%</span> </h3> <div class="progress progress-xxs" style="height:12px;"> <div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="78" aria-valuemin="0" id="third" aria-valuemax="100" style="width: 78%"> </div> </div> </div> <!-- End Thin Size Progress Bars --> <div class="caption"> <h3 style="color:#c0392b;text-align:center;padding-bottom:16px;">Caracteristici: <i class="fa fa-mobile"></i></h3> <!------ THIS IS THE JQUERY SLIDE ----------> <div class="flip">Apasa pentru detalii</div> <div class="panel"> <p class="text-bold">Display Super AMOLED</p> <p>6.2 inch cu rezoluție 2960×1440 pixeli, aspect 18.5:9, 529 ppi, protejat cu sticlă Corning Gorilla Glass 5, 3D Touch pentru butonul de Home, certificare IP68 în ce privește contactul cu apa și praful, raport display / corp de 84.2%;</p> <p class="text-bold">Procesor Exynos 9810 Octa</p> <p> 4 x Mongoose M3 la 2.8 GHz și 4 x ARM Cortex-A55 la 1.7 GHz și Mali-G72 MP18 sau Qualcomm Snapdragon 845 de tip octa-core cu 4 x Kryo 385 la 2.7 GHz și 4 x Kryo 385 Silver la 1.7 GHz, 6 GB RAM și 64 / 128 / 256 GB spațiu de stocare, slot pentru cardurile microSD de până la 400 GB;</p> <p class="text-bold">Android 8.0 Oreo</p> <p>4G cu viteze de până la 1200 Mbps în download și 200 Mbps în upload, Wi-Fi a/b/g/n/ac, dual-band, Wi-Fi direct, hotspot, bluetooth 5.0, USB type-C, NFC, jack de 3.5 mm, A-GPS, GLONASS, BDS și GALILEO;</p> <p class="text-bold">Camera</p> <p>12 Megapixeli cu f/1.5 sau 2.4, dimensiune a pixelului de 1.4 microni + 12 Megapixeli cu f.2.4, zoom optic 2x, Dual Pixel PDAF, detecție de fază pentru autofocus, OIS, LED flash, filmează la 2160p cu 30 fps, filmează la 720p cu 960 fps, cameră frontală de 8 Megapixeli cu f/1.6 ce filmează la 1440p;</p> <p class="text-bold">Baterie</p> <p>3500 mAh, 189 gr. și dimensiuni de 158.1 x 73.8 x 8.5 mm grosime.</p> <p class="text-bold" style="font-size:14px;">Pret: 600<i class="fa fa-eur"></i></p> <div align="right"> <div class="button"> <a href="https://www.gsmarena.com/samsung_galaxy_s9+-8967.php" class="btn btn-one"> Mai multe </a></div> </div> </div> </div> </div> </div> </div> <!---- NUMARUL 9 -----> <div class="container-fluid"> <div class="col-sm-6 col-md-offset-3"> <h2 id="primul" style="color:darkmagenta;"> 10. Samsung S9+ </h2> <div class="thumbnail"> <img src="telefoane/s9.jpg" alt="Samsung S9+"> <!-- Thin Size Progress Bars --> <div class="margin"> <h3 class="progress-label">Rapiditate <span class="pull-right">85%</span> </h3> <div class="progress progress-xxs" style="height:12px;"> <div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="92" aria-valuemin="0" id="first" aria-valuemax="100" style="width: 92%"> </div> </div> <h3 class="progress-label">Rezistenta <span class="pull-right">68%</span> </h3> <div class="progress progress-xxs" style="height:12px;"> <div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="68" aria-valuemin="0" id="second" aria-valuemax="100" style="width: 68%;"> </div> </div> <h3 class="progress-label">Performanta <span class="pull-right">81%</span> </h3> <div class="progress progress-xxs" style="height:12px;"> <div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="78" aria-valuemin="0" id="third" aria-valuemax="100" style="width: 78%"> </div> </div> </div> <!-- End Thin Size Progress Bars --> <div class="caption"> <h3 style="color:#c0392b;text-align:center;padding-bottom:16px;">Caracteristici: <i class="fa fa-mobile"></i></h3> <div class="flip">Apasa pentru detalii</div> <div class="panel"> <p class="text-bold">Display Super AMOLED</p> <p>6.2 inch cu rezoluție 2960×1440 pixeli, aspect 18.5:9, 529 ppi, protejat cu sticlă Corning Gorilla Glass 5, 3D Touch pentru butonul de Home, certificare IP68 în ce privește contactul cu apa și praful, raport display / corp de 84.2%;</p> <p class="text-bold">Procesor Exynos 9810 Octa</p> <p> 4 x Mongoose M3 la 2.8 GHz și 4 x ARM Cortex-A55 la 1.7 GHz și Mali-G72 MP18 sau Qualcomm Snapdragon 845 de tip octa-core cu 4 x Kryo 385 la 2.7 GHz și 4 x Kryo 385 Silver la 1.7 GHz, 6 GB RAM și 64 / 128 / 256 GB spațiu de stocare, slot pentru cardurile microSD de până la 400 GB;</p> <p class="text-bold">Android 8.0 Oreo</p> <p>4G cu viteze de până la 1200 Mbps în download și 200 Mbps în upload, Wi-Fi a/b/g/n/ac, dual-band, Wi-Fi direct, hotspot, bluetooth 5.0, USB type-C, NFC, jack de 3.5 mm, A-GPS, GLONASS, BDS și GALILEO;</p> <p class="text-bold">Camera</p> <p>12 Megapixeli cu f/1.5 sau 2.4, dimensiune a pixelului de 1.4 microni + 12 Megapixeli cu f.2.4, zoom optic 2x, Dual Pixel PDAF, detecție de fază pentru autofocus, OIS, LED flash, filmează la 2160p cu 30 fps, filmează la 720p cu 960 fps, cameră frontală de 8 Megapixeli cu f/1.6 ce filmează la 1440p;</p> <p class="text-bold">Baterie</p> <p>3500 mAh, 189 gr. și dimensiuni de 158.1 x 73.8 x 8.5 mm grosime.</p> <p class="text-bold" style="font-size:14px;">Pret: 600<i class="fa fa-eur"></i></p> <div align="right"> <div class="button"> <a href="https://www.gsmarena.com/samsung_galaxy_s9+-8967.php" class="btn btn-one"> Mai multe </a></div> </div> </div> </div> </div> </div> </div>作为utc_time,CONVERT_TZ({{1},'UTC','CET')作为local_time FROM    JAXBContext ctx = JAXBContext.newInstance(YOUR_CLASS.class); Unmarshaller unmarshaller = ctx.createUnmarshaller(); SAXParserFactory sax = SAXParserFactory.newInstance(); sax.setNamespaceAware(false); // This line is important! XMLReader reader = sax.newSAXParser().getXMLReader(); Source source = new SAXSource(reader, new InputSource(new StringReader(xml))); return (YOUR_CLASS) unmarshaller.unmarshal(source); ;

答案 1 :(得分:1)

除非我缺少任何东西,否则CONVERT_TZ() 正是您所需要的:

SELECT CONVERT_TZ('2018-01-05 07:00:00', 'UTC', 'Europe/Warsaw');
2018-01-05 08:00:00

Demo

唯一的缺点是,许多服务器似乎缺乏使用命名时区的适当配置(不确定您是否担心的问题)。

PHP替代方法应该可以立即使用:

$utc = new DateTimeZone('UTC');
$local = new DateTimeZone('Europe/Warsaw');
$dt = new DateTime('2018-01-05 07:00:00', $utc);
echo $dt->format('c'), PHP_EOL;
$dt->setTimezone($local);
echo $dt->format('c'), PHP_EOL;
2018-01-05T07:00:00+00:00
2018-01-05T08:00:00+01:00

Demo

P.S。如果您的GPS跟踪器始终生成相同的数据格式,则实际上没有理由使用VARCHAR,因为它是MySQL期望的确切日期格式。