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条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题