gpt4 book ai didi

php - 使用 TIMESTAMPDIFF PHP MYSQL 同时更新结束时间和持续时间

转载 作者:行者123 更新时间:2023-11-30 00:04:07 25 4
gpt4 key购买 nike

我想在同一查询/操作中更新“持续时间”和“end_time”字段。 “end_time”字段填充当前时间,“持续时间”字段填充“start”和“end_time”之间的不同分钟>' 字段。当我执行此更新时,“duration”字段中的结果为0。如何同时获取end_time和duration。

这是 php 代码:

<?php
include("koneksi.php");
$id = $_GET['id'];
$start = gmdate("Y-m-d H:i:s", time()+60*60*7);
$end_time = gmdate("Y-m-d H:i:s", time()+60*60*7);
$duration = $_POST['duration'];


$query = "update billing set end_time='$end_time', duration = TIMESTAMPDIFF(MINUTE, '$start', '$end_time') where id='$id'";

$result = mysql_query($query);
if ($result){
echo '<script language="javascript">window.location = "../?p=los"</script>';
} ?>

最佳答案

持续时间设置为 0,因为 $start$end_time 相同。

考虑你的代码:

$start  = gmdate("Y-m-d H:i:s", time()+60*60*7);
$end_time = gmdate("Y-m-d H:i:s", time()+60*60*7);

$query = "... TIMESTAMPDIFF(MINUTE, '$start', '$end_time') ...";

因为$start == $end_time,差值始终为0。

您可能想要使用已存储在数据库中的 start_time 值,而不是最近创建的 php 变量 $start。也许是这样的:

$query = "
update billing set
end_time='$end_time',
duration = TIMESTAMPDIFF(MINUTE, start_time, '$end_time')
where id='$id'
";

其中 start_time 是数据库中的一列。

<小时/>

注意: mysql_* functions are deprecated ,并且您容易受到 SQL injection 的影响。考虑使用mysqlipdo并利用prepared statements .

关于php - 使用 TIMESTAMPDIFF PHP MYSQL 同时更新结束时间和持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24750082/

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