如何在 PHP/MySQL 中增加 4 小时的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1661631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:26:04  来源:igfitidea点击:

How to add 4 hours to time in PHP/MySQL

phpmysqldatetime

提问by TWLATL

I'm working on a blog migration from a custom built blog to Wordpress. One of the fields that Wordpress is looking for in the database is a date/time stamp set to GMT, which is 4 hours ahead of our time. So I basically need to take our date/time stamp (in YYYY-MM-DD HH:MM:SS format), and add four hours to it. I was looking at the MySQL command "ADDTIME", but I think that only works on selects, not on inserts.

我正在将博客从自定义构建的博客迁移到 Wordpress。Wordpress 在数据库中查找的字段之一是设置为 GMT 的日期/时间戳,它比我们的时间提前 4 小时。所以我基本上需要获取我们的日期/时间戳(以 YYYY-MM-DD HH:MM:SS 格式),并添加四个小时。我正在查看 MySQL 命令“ADDTIME”,但我认为它仅适用于选择,而不适用于插入。

I had worked up a script that exploded the date in to parts, and added 4 hours to the time, but the ensuing logic that would be required to check for when 4 hours pushes in to the next day/month/year seemed a little excessive.

我已经编写了一个脚本,将日期分解为多个部分,并增加了 4 小时的时间,但是随后需要检查 4 小时推入第二天/月/年的逻辑似乎有点过分.

回答by moo

date($format, strtotime("$date + 4 hours"));

回答by artlung

There's nothing that prevents ADDTIME()from being used in an INSERTOR UPDATE, but DATE_ADD()is probably what will work best:

没有什么可以阻止ADDTIME()INSERTOR 中使用UPDATE,但DATE_ADD()可能最有效:

INSERT INTO table_name
SET my_datetime = DATE_ADD('2009-11-01 19:30:00', INTERVAL 4 HOURS),
...other insert columns here... ;

回答by Andreas

What about:

关于什么:

date( "Y-m-d H:i:s", strtotime( "2009-08-09 23:44:22" )+4*60*60 )

or even

甚至

date( "Y-m-d H:i:s", strtotime( "2009-08-09 23:44:22 + 4 hours" ) )

Might need a bit of error checking, but should solve your problem.

可能需要进行一些错误检查,但应该可以解决您的问题。

回答by Christophe Eblé

Or better in SQL

或者在 SQL 中更好

DATE(DATE_ADD(`Table`.`Column`, INTERVAL 4 HOURS))

回答by iisystems

For all the answers using DATE_ADD, the correct syntax is "4 HOUR" not "4 HOURS" (at least in the current version of MySQL):

对于使用 DATE_ADD 的所有答案,正确的语法是“4 HOUR”而不是“4 HOURS”(至少在当前版本的 MySQL 中):

UPDATE table SET field = DATE_ADD(field, INTERVAL 4 HOUR);

UPDATE table SET field = DATE_ADD(field, INTERVAL 4 HOUR);

回答by dar7yl

It looks like the TZ of your database is set to GMT (UTC), which is as it should be. You need to convert your local date into GMT when adding to the database.

看起来您的数据库的 TZ 设置为 GMT (UTC),这是应该的。添加到数据库时,您需要将本地日期转换为 GMT。

from the MySQL 5.1 Reference Manual:

来自 MySQL 5.1 参考手册:

CONVERT_TZ(dt,from_tz,to_tz)

CONVERT_TZ() converts a datetime value dt from time zone given by from_tz to the time zone given by to_tz and returns the resulting value. Time zones may be specified as described in Section 5.10.8, “MySQL Server Time Zone Support”. This function returns NULL if the arguments are invalid.

CONVERT_TZ() 将日期时间值 dt 从 from_tz 给定的时区转换为 to_tz 给定的时区,并返回结果值。可以按照第 5.10.8 节,“MySQL 服务器时区支持”中的描述指定时区。如果参数无效,此函数返回 NULL。

If the value falls out of the supported range of the TIMESTAMP type when converted fom from_tz to UTC, no conversion occurs. The TIMESTAMP range is described in Section 11.1.2, “Overview of Date and Time Types”.

如果该值在从 from_tz 转换为 UTC 时超出 TIMESTAMP 类型的支持范围,则不会发生转换。TIMESTAMP 范围在第 11.1.2 节“日期和时间类型概述”中描述。

mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
        -> '2004-01-01 13:00:00'

mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');
        -> '2004-01-01 22:00:00'

回答by Sebanza

I have used this method:

我用过这个方法:

$format = Y-m-d; //you can use any format if you want
$date = $row['date']; // from mysql_fetch_array
$date2 = date($format, strtotime("$date + 4 week"));
echo $date;

回答by ferreirabraga

If you use (day/month/year)[Brazilian date format] as below, it doesn't work:

如果您使用 (day/month/year)[巴西日期格式] 如下,它不起作用:

$todays_date = date("d/m/Y H:i:s");

echo "Server date/hour ". $todays_date;

echo "Server date/hour + 2 hours ".date("d/m/Y H:i:s", strtotime("$todays_date + 2 hours"));

$todays_date = date(" d/m/YH:i:s");

回声“服务器日期/小时”。$todays_date;

echo "服务器日期/小时 + 2 小时 ".date(" d/m/YH:i:s", strtotime("$todays_date + 2 hours"));

Result:

结果:

Server date/hour                  10/04/2012 07:44:36

Server date/hour+ 2 hours    04/10/2012 09:44:36

服务器日期/小时                  10/04/2012 07:44:36

服务器日期/小时+ 2 小时    04/10/2012 09:44:36



You need to you (month/day/year) and it works perfect:

你需要你(月/日/年),它完美地工作:

$todays_date = date("m/d/Y H:i:s"); echo "Server date/hour ".

$todays_date."
"; echo ""Server date/hour + 2 hours ".date("m/d/Y H:i:s", strtotime("$todays_date + 2 hours"));

$todays_date = date(" m/d/YH:i:s"); 回声“服务器日期/小时”。

$todays_date."
"; echo ""服务器日期/小时 + 2 小时 ".date(" m/d/YH:i:s", strtotime("$todays_date + 2 hours"));

Result:

结果:

Server date/hour                   04/10/2012 07:44:36

Server date/hour+ 2 hours    04/10/2012 09:44:36

服务器日期/小时                   04/10/2012 07:44:36

服务器日期/小时+ 2 小时    04/10/2012 09:44:36

回答by Adam IS

Working 100%

工作 100%

<?php

$datee= date("Y-m-d", strtotime("$last_update+ 10 hours"));
$timee= date("H:i:s", strtotime("$last_update+ 10 hours"));
?>

Last update was on: <?php  echo $datee; ?>  at  <?php  echo $timee; ?>

$last_update is a value contents timestamp from mysql

$last_update 是来自 mysql 的值内容时间戳

The result will be like this "Last update was on: 2015-02-05 at 05:01:43"

结果将是这样的“上次更新时间:2015-02-05 05:01:43”

回答by Viktor

<?php

function data_time($DT, $pm, $val, $time_type){
    $time = date("H:i:s");
    $date = date("d.m.Y");

    if($DT=='d'){
        switch($pm){
            case '+': $newData = date("d.m.Y ", strtotime("$date + $val $time_type"));
            break;
            case '-': $newData = date("d.m.Y ", strtotime("$date - $val $time_type"));
            break;
        }
        return $newData;
    }
    if($DT=='t'){
        switch($pm){
            case '+': $newTime = date("H:i:s ", strtotime("$time + $val $time_type"));
            break;
            case '-': $newTime = date("H:i:s ", strtotime("$time - $val $time_type"));
            break;
        }
        return $newTime;
    }
}

$newData = data_time('t','+',5,'year')
//or

$DataTime = 'd'; // 't' or 'd'
$PlusMinus ='+'; // '+' or '-'
$val = 1;        // 0-9
$DMT ='month';   // 'day', 'month' or 'year'

$newData = data_time($DataTime, $PlusMinus, $val, $DMT);
echo $newData;

?>