douzong5057 2014-07-15 04:54
浏览 56
已采纳

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

I want to update 'duration' and 'end_time' field in same query/action. 'end_time' field filled current time, 'duration' field filled from different minute between 'start' and 'end_time' field. when i execute this update, the result in 'duration' field is 0. How to get end_time and duration in same time.

this is the php code :

<?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>'; 
} ?>
  • 写回答

2条回答 默认 最新

  • doufang3001 2014-07-15 05:02
    关注

    Duration is being set to 0 because $start and $end_time are the same.

    Consider your code:

    $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') ...";
    

    Because $start == $end_time, the difference will always be 0.

    You probably want to use the value of start_time already stored in the database, not the recently created php variable $start. Perhaps something like this:

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

    where start_time is a column in the database.


    Note: mysql_* functions are deprecated, and you are susceptible to SQL injection. Consider using mysqli or pdo and utilize prepared statements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器