【问题标题】:How can i manipulate MySQL Datetime with php, (adding hours and converting to timezone)?如何使用 php 操作 MySQL 日期时间(添加小时数并转换为时区)?
【发布时间】:2025-12-26 08:30:16
【问题描述】:

在 ma DB 中,我有一个包含五列行的表: 1.开始时间 2. 结束时间 3. 计划开始时间 4. 计划持续时间 5. 时区

现在我想根据这个逻辑实际返回开始和结束时间:

对于已确定的事件:

  • 如果 start-time "not" '0000-00-00 00:00:00',则使用 start-time as -> start
  • 如果结束时间 "not"'0000-00-00 00:00:00',则使用结束时间作为 -> end

对于计划中的活动:

  • 如果开始时间 "is" '0000-00-00 00:00:00',则使用计划开始时间作为 开始
  • 如果结束时间 "is" '0000-00-00 00:00:00',则使用计划开始时间 + 持续时间(即分钟:: expl: 200 分钟。)作为结束

现在我的时区有这个 json,如下所示:

   { 
      dstOffset: 0
      rawOffset: 3600
      timeZoneId: "Europe/Zurich"
      timeZoneName:"Central European Standard Time"
    }

现在我想将我从上述方法获得的时间转换为这个时区,然后像这样输出:'2017-03-01 00:00:00'

最好的方法是什么?

【问题讨论】:

    标签: php mysql datetime timezone


    【解决方案1】:

    您需要将 unix 时间戳转换为 mysql 时间戳。

    $timeToAdd = 200; // Number of seconds
    $timeToAdd = 60 * 60; // 1 hours (3600 seconds)
    
    //using one of the above $timeToAdd you can calculate it using the following
    $mysql_time = date("Y-m-d H:i:s", time() + $timeToAdd);
    

    $mysql_time 现在包含 mysql 格式的时间戳

    例如:2017-03-19 09:22:06

    【讨论】: