doujuanju3076 2015-02-23 13:55 采纳率: 0%
浏览 384
已采纳

使用Bootstrap-Editable时,日期时间更新出错

I'm trying to update a sql datetime. I just wana update the time and have some scripts for substr(). Now when i update the Hrs and min the day (d) get's reset to 1 in the first edit and 0 in a second edit.

I'm using a html table generated by php and bootstrap-editable for editing the rows using Js and PHP.

Why does the date return first 01 and then 00 in db?

Ex. 2012-02-23 00:00:00 becomes 2012-02-01 00:00:00 in first update.

HTML

<a href='#' id='".$row['id']."' class='usr_stamp_out' data-name='usr_stamp_out' data-type='text' data-pk='".$row['id']."' data-url='php/time.php' data-title='Stämpla ut..'>".substr($row['usr_stamp_out'],11,5)."</a>

PHP

$id = $_POST['pk'];
$updated_usr_stamp_out = $_POST['value'];

if($row){
    //Substring datetime to 0000-00-00
    $usr_stamp_out_date = substr($row['usr_stamp_out'],0,9);

    //Add old date to updated time
    $new_usr_stamp_out = $usr_stamp_out_date." ".$updated_usr_stamp_out;

    //Prepare query
    $query = "UPDATE table SET usr_stamp_out = :usr_stamp_out WHERE id = :id";

    // Security measures
    $query_params = array(':id' => $id,':usr_stamp_out' => $new_usr_stamp_out);

    //Connect and execute
    try {  
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex){
        die("Failed to run query: " . $ex->getMessage());
    }
}
  • 写回答

1条回答 默认 最新

  • dqqt31923 2015-02-23 13:58
    关注

    $usr_stamp_out_date = substr($row['usr_stamp_out'],0,9); is only getting the first nine characters of the date instead of the ten needed to have the correct date.

    $usr_stamp_out_date = substr($row['usr_stamp_out'],0,10);
    

    A alternative way to do this is to just get the date using PHP's date functionality:

    $usr_stamp_out_date = date('Y-m-d', strtotime($row['usr_stamp_out']));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Power BI 里面 帕累托图突出显示前20
  • ¥50 用预估矫正法,分数阶微分方程组传染病的最优控制代码怎么写
  • ¥15 画个数据流程图,手画也行
  • ¥60 AS自带模拟器AVD Root 和 Xposed安装
  • ¥15 Esp32的microPython设备中main.py文件无法开机自启动
  • ¥30 哪位搞Android的编程师可以帮我远程一下,悬赏30元
  • ¥15 solidity部署上合约,可以mint,也继承接口了,在区块链也不显示代币名字
  • ¥15 讨论 博士论文交互项怎么讨论?
  • ¥50 在集成BiLSTM和GCN模型时遇到了问题
  • ¥33 集成BiLSTM模型和GCN模型时,前模型的输出不匹配后模型的输入