Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中添加时间_Php_Mysql_Datetime_Time_Compare - Fatal编程技术网

在PHP中添加时间

在PHP中添加时间,php,mysql,datetime,time,compare,Php,Mysql,Datetime,Time,Compare,我正在从mysql数据库中提取一个日期时间,我想在其中添加X小时,然后将其与当前时间进行比较。到目前为止我 $dateNow = strtotime(date('Y-m-d H:i:s')); $dbTime = strtotime($row[0]); 然后我尝试$dbTime+strottime(“4小时”);但是,4小时似乎会使当前时间增加4小时,而不是原始的4小时。如何向dbTime添加X小时 注意:我使用的是php 5.1.2,因此date_add不起作用(5.3.0)我倾向于使用ti

我正在从mysql数据库中提取一个日期时间,我想在其中添加X小时,然后将其与当前时间进行比较。到目前为止我

$dateNow = strtotime(date('Y-m-d H:i:s'));
$dbTime = strtotime($row[0]);
然后我尝试$dbTime+strottime(“4小时”);但是,4小时似乎会使当前时间增加4小时,而不是原始的4小时。如何向dbTime添加X小时

注意:我使用的是php 5.1.2,因此date_add不起作用(5.3.0)

我倾向于使用time()函数,手册中的这一页显示了他们未来一周的日期: 以下是我的做法:

  • 使用函数从数据库中提取时间

  • query("SELECT UNIX_TIMESTAMP(someDatetimeColumn) ...");
    . . .
    $dbTimeAdjusted = localtime($row[0] + 4*60*60);
    
  • UNIX时间戳以秒为单位,因此添加
    4*60*60

  • 使用PHP的or函数将修改后的UNIX时间戳转换为日期

    query("SELECT UNIX_TIMESTAMP(someDatetimeColumn) ...");
    . . .
    $dbTimeAdjusted = localtime($row[0] + 4*60*60);
    
  • time()和strottime()以秒为单位生成unix时间戳,因此您可以执行以下操作,前提是提供数据库并进行比较:

    $fourHours = 60 * 60 * 4;
    $futureTime = time() + $fourHours;
    
    然后我尝试$dbTime+strottime(“4小时”);但是,4小时似乎会使当前时间增加4小时,而不是原始的4小时。如何向dbTime添加X小时

    strotime
    有一个可选的第二个参数。在那里提供Unix时间戳,输出将相对于该日期而不是当前日期

    $newTime = strtotime('+4 hours', $dbTime);
    

    您还可以使用Unix时间戳是以秒为基础的这一事实—如果您知道以秒为单位的四小时是多少,您只需将其添加到时间整数值中即可。

    这里有很多选项:

    一,

    二,

    三,

    四,

    五,

    STROTIME(“+4小时,$dbTime”)

    第二个参数是时间戳,它被用作计算相对日期的基础;它默认为当前时间。看看这本书

    编辑:
    对于最长1周的短时间段,在时间戳上添加秒是完全可以接受的。每周总有(7*24*3600)秒;一个月或一年内都不能这样说。此外,unix时间戳只是自unix纪元(1970年1月1日00:00:00 GMT)以来经过的秒数。这不受时区或夏令时的影响。时区和夏令时仅在将unix时间戳转换为实际日历日和时间时才重要。

    可能最安全的比较方法是在SQL中进行比较

    SELECT * FROM my_table WHERE someDateTimeColumn < DATE_ADD(NOW(), INTERVAL 4 hour)
    
    从my_表中选择*,其中someDateTimeColumn
    由于您是在PHP中组装它,所以您可以用代码需要比较的任何内容动态地替换“4小时”位


    (注意:将整个计算放在与列比较的另一端,允许MySQL在每个查询中进行一次计算,而不是每行进行一次,并且如果该列有索引,还可以使用表的索引。)

    假设DB返回的时间戳是SQL格式的,那么以下操作应该可以正常工作:

    $dbTime = strtotime($row[0]);
    $nowTime = time();
    
    $future_dbTime = strtotime("+4 hours", $dbTime);
    
    $diff_time_seconds = $nowTime - $dbTime;
    
    if ($diff_time_seconds > 0) {
           echo "The current time is greater than the database time by:\n";
           $not_equal = true;
    
        }
    if ($diff_time_seconds == 0) {
           echo "The current time is equal to the database time!";
        }
    if ($diff_time_seconds < 0) {
           echo "The current time is less than the database time by:\n";
           $not_equal = true;
        }
    
    if ($not_equal) {
    $diff_time_abs_seconds = abs($diff_time_seconds);
    echo date('h:m:s', $diff_time_abs_seconds);
    }
    
    $dbTime=strottime($row[0]);
    $nowTime=time();
    $future_dbTime=strottime(“+4小时,$dbTime”);
    $diff_time_seconds=$nowTime-$dbTime;
    如果($diff_time_seconds>0){
    echo“当前时间比数据库时间大:\n”;
    $not_equal=真;
    }
    如果($diff_time_seconds==0){
    echo“当前时间等于数据库时间!”;
    }
    如果($diff_time_seconds<0){
    echo“当前时间比数据库时间短:\n”;
    $not_equal=真;
    }
    如果($不相等){
    $diff\u time\u abs\u seconds=abs($diff\u time\u seconds);
    回显日期('h:m:s',$diff\u time\u abs\u seconds);
    }
    
    如果我能否决你问题的一部分,我会的#2是你发布的最糟糕的解决方案。真的吗?我本以为1是最糟糕的。作为记录,我个人可能会使用#3或#5,这取决于我对构建SQL查询的控制程度。请参阅ceejayoz答案中的注释,了解我为什么这么认为。不要只在unixtime中添加秒数!这是开始进行所有时间/日期操作的一个滑动斜率的开始,并且很难维护和调试,特别是考虑时区和日光节约。有一些库函数可以纠正所有困难的拐角和边缘情况。只需在unixtime中添加秒数即可!很明显,这就是它的用途,在unix时间戳上添加秒是完全可以接受的。它是从一个纪元日期算起的秒数,时区的任何变化或夏令时都不会影响它。TZs和DST将影响人类可读的版本,但时间戳不受影响。它取决于原始时间戳的上下文以及为什么要将其与“现在”进行比较,这两种情况我们都没有。如果它是X小时的直接转换,并且该转换在算法中是有意义的,那么只需在unix时间戳中添加秒就有意义。不幸的是,我见过太多这样做的代码,即使它没有意义或不清楚,并且必须修复由此产生的错误。如果你想知道当前时间在不同的时区,那么不,你永远不应该加/减秒。如果你想知道4小时内的时间是多少,那么时间戳算法是完美的解决方案。“一周内总有(7*24*3600)秒。”不是真的!两者都会改变一周的秒数。
    $fourHoursAhead = strtotime("+4 hours", $myDate);
    
    $result = mysql_query("SELECT UNIX_TIMESTAMP(DATE_ADD(myDate, INTERVAL 4 HOUR))");
    $fourHoursAhead = mysql_result($result, 0);
    
    SELECT * FROM my_table WHERE someDateTimeColumn < DATE_ADD(NOW(), INTERVAL 4 hour)
    
    $dbTime = strtotime($row[0]);
    $nowTime = time();
    
    $future_dbTime = strtotime("+4 hours", $dbTime);
    
    $diff_time_seconds = $nowTime - $dbTime;
    
    if ($diff_time_seconds > 0) {
           echo "The current time is greater than the database time by:\n";
           $not_equal = true;
    
        }
    if ($diff_time_seconds == 0) {
           echo "The current time is equal to the database time!";
        }
    if ($diff_time_seconds < 0) {
           echo "The current time is less than the database time by:\n";
           $not_equal = true;
        }
    
    if ($not_equal) {
    $diff_time_abs_seconds = abs($diff_time_seconds);
    echo date('h:m:s', $diff_time_abs_seconds);
    }