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

标签 php html mysql datetime x-editable

我正在尝试更新 SQL 日期时间。我只是想更新时间并有一些 substr() 脚本。现在,当我更新小时和分钟(d)时,在第一次编辑中重置为 1,在第二次编辑中重置为 0。

我使用 php 和 bootstrap-editable 生成的 html 表来使用 Js 和 PHP 编辑行。

为什么日期在db中先返回01,然后返回00?

例如。第一次更新时,2012-02-23 00:00:00 变为 2012-02-01 00:00:00。

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());
    }
}

最佳答案

$usr_stamp_out_date = substr($row['usr_stamp_out'],0,9); 仅获取日期的前九个字符,而不是获得正确日期所需的十个字符。

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

另一种方法是使用 PHP 的日期功能获取日期:

$usr_stamp_out_date = date('Y-m-d', strtotime($row['usr_stamp_out']));

关于php - 使用 Bootstrap-Editable 时日期时间更新出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28675394/

相关文章:

javascript - 不符合要求的文本区域如何显示红色边框?

android - 为什么我的MySql数据库数据没有显示在应用程序中?

sql - 按某些列排序,在 MySQL 中也按 rand() 排序

php - 我应该限制或减少我的数据库查询吗?

php - 带有页脚的 Wordpress 网站覆盖一页上的内容。

iphone - HTML <select> 元素在 iPhone 或 Android 浏览器中被缩写

mysql - MySQL 中自动命名列

php - 扩展 DOMElement 对象

php - 使用 HTML FORM 和 PHP 使用 POST 方法将数据添加到 MYSQL

html - 媒体查询覆盖桌面样式