gpt4 book ai didi

php - Mysql 搜索表查找日期时间字段并更改时区

转载 作者:行者123 更新时间:2023-11-29 19:28:29 25 4
gpt4 key购买 nike

我的旧数据库中有一些表以 CST 格式存储日期。现在我想将它们存储在 UTC 中。

我想在 mysql 中创建一个脚本/查询,它将在我的数据库中搜索带有 datetime 字段的表,然后将其中的值转换为 UTC。

无需编写表名即可执行此操作的脚本会很有趣。

I have the query to convert time zone with CONVERT_TZ, that is not a problem with me. But I want a script that searches through the database for tables and fields that has datetime as data type and then run the script on those fields.

知道我该怎么做吗?任何引用都很好,不一定是代码。

最佳答案

首先获取列,遍历数组以确定它是否与您想要的Type匹配。

然后进行更新就好了。

<?php
$db = new Mysqli("host", "user", "pass", "db");

$column_query = $db->query("SHOW COLUMNS IN table");
if ($column_query->num_rows != 0){
while ($column = $column_query->fetch_object()){
if ($column->Type != 'datetime')
continue;
$db->query("UPDATE table SET {$column->Field}=CONVERT_TZ({$column->Field}, 'timezoneFrom', 'timezoneTo' )");
}
}

关于php - Mysql 搜索表查找日期时间字段并更改时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41975371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com