Hướng dẫn convert_tz mysql

Last update on August 19 2022 21:50:41 (UTC/GMT +8 hours)

CONVERT_TZ() function

In MySQL the CONVERT_TZ() returns a resulting value after converting a datetime value from a time zone specified as the second argument to the time zone specified as the third argument. This function returns NULL when the arguments are invalid.

Syntax:

CONVERT_TZ (dt, from_tz,to_tz)

Arguments:

NameDescription
dt A datetime.
from_tz A time zone which will be converted to to_tz.
to_tz A time zone in which the from_tz will convert.

Syntax Diagram:

Hướng dẫn convert_tz mysql

MySQL Version: 5.6

Video Presentation:

Your browser does not support HTML5 video.

Pictorial Presentation:

Hướng dẫn convert_tz mysql

Example: MySQL CONVERT_TZ() function

The following statement will convert the datetime value 2008-05-15 12:00:00 from +00:00 timezone to +10:00 timezone.

Code:

SELECT CONVERT_TZ('2008-05-15 12:00:00','+00:00','+10:00');

Sample Output:

mysql> SELECT CONVERT_TZ('2008-05-15 12:00:00','+00:00','+10:00');
+-----------------------------------------------------+
| CONVERT_TZ('2008-05-15 12:00:00','+00:00','+10:00') |
+-----------------------------------------------------+
| 2008-05-15 22:00:00                                 | 
+-----------------------------------------------------+
1 row in set (0.02 sec)

PHP script:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>example-CONVERT_TZ-function - php mysql examples | w3resource</title>
<meta name="description" content="example-CONVERT_TZ-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Calculating a datetime, converting 2008-05-15 12:00:00 to a time zone which is ten hours ahead using MySQL:</h2>
<table class='table table-bordered'>
<tr> 
<th>Required Date</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password"; 
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT CONVERT_TZ("2008-05-15 12:00:00","+00:00","+10:00") as required_datetime') as $row) {
echo "<tr>";
echo "<td>" . $row['required_datetime'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

JSP script:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-convert_tz-function</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data = "SELECT CONVERT_TZ('2008-05-15 12:00:00','+00:00','+10:00') as required_datetime";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Required Date</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("required_datetime")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

All Date and Time Functions :

Click here to see the MySQL Date and time functions.

Previous: ADDTIME()
Next: CURDATE()

Nếu điều này cho kết quả là null thì các bảng TZ chưa được thiết lập:

SELECT CONVERT_TZ(now(),'US/Eastern','US/Central');

Nếu bạn chưa thiết lập bảng múi giờ, bạn có thể cập nhật chênh lệch giờ trong bảng người dùng và sau đó thực hiện:

select utc_timezone() - interval user_timezone_offset_in_hours hour
from userinfo a
where user_id = 999;

Tuy nhiên, bạn vẫn cần một cách để cập nhật múi giờ của người dùng.

Nếu bạn đang viết điều này cho một ứng dụng web, bạn có thể lấy múi giờ thông qua javascript, đây là một bài viết mô tả cách thực hiện (chưa thử cách này nhưng có vẻ như nó sẽ hoạt động).

Một chút giải thích liên quan đến 'khoảng thời gian' ở trên ...

Một trong những cấu trúc thủ thuật khác trong MySQL là sử dụng INTERVAL từ khóa, được hiển thị tốt nhất bằng ví dụ (giá trị số có thể là một biểu thức hoặc giá trị trường)

select now() today, now() - interval 1 day yesterday;
+---------------------+---------------------+
| today               | yesterday           |
+---------------------+---------------------+
| 2011-05-26 13:20:55 | 2011-05-25 13:20:55 |
+---------------------+---------------------+

Bạn có thể thêm và trừ chúng theo cách nào bạn thích, đây là lý do tại sao tôi không bao giờ bận tâm đến ngày / giờ các hàm cộng / trừ / chuyển đổi

select now() a, now() - interval 1 day + interval 4 hour + interval 8 minute b;
+---------------------+---------------------+
| a                   | b                   |
+---------------------+---------------------+
| 2011-05-26 13:24:16 | 2011-05-25 17:32:16 |
+---------------------+---------------------+

Bạn có thể sử dụng các số âm (sẽ tốt cho việc chênh lệch múi giờ âm), chúng giống nhau:

select now() - interval 1 month a, now() + interval -1 month b;
+---------------------+---------------------+
| a                   | b                   |
+---------------------+---------------------+
| 2011-04-26 13:38:05 | 2011-04-26 13:38:05 |
+---------------------+---------------------+

33 hữu ích 3 bình luận chia sẻ