【问题标题】:how to keep date as the uk standard but still input to database correct如何将日期保持为英国标准但仍正确输入数据库
【发布时间】:2017-05-20 21:07:27
【问题描述】:

我正在尝试向我的数据库添加一个日期 我的表单上有一个 jQuery 数据选择器,但它添加了一个类似 M - d - y 的日期,我的数据库不接受它。在添加到数据库之前,我需要添加一些内容来更改日期。

date('Y-m-d H:i:s', strtotime($date)); 

我不知道我需要在哪里添加任何想法

<?php include('connect2.php'); ?>

<!doctype html>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="jqueryui/jquery-ui.css">
    <link type="text/css" rel="stylesheet" href="jqueryui/jquery-ui.structure.css">
    <link type="text/css" rel="stylesheet" href="jqueryui/jquery-ui.theme.css">
    <link type="text/css" rel="stylesheet" href="jqueryui/jquery.timepicker.css">




<body>


<meta charset="utf-8">
<title>index.php</title>
</head>

<body>

<pre>
    <?php

    if (isset ($_POST['submit'] )) {



    /*$query = "INSERT INTO visitors (firstname, lastname, time, date, email, staff) VALUES ('$_POST[firstname]', '$_POST[lastname]', '$_POST[time]', '$_POST[date]', '$_POST[email]', '$_POST[staff]')";
    $result = mysqli_query($dbc, $query);
    }*/


        $statement = $db->prepare("INSERT INTO visitors (firstname, lastname, time, date, email, staff) VALUES (:firstname, :lastname, :time, :date, :email, :staff)");

        $statement->execute(array(


            "firstname" => $_POST['firstname'],

            "lastname"  => $_POST['lastname'],

            "time" => $_POST['time'],

            "date" => $_POST['date'],

            "email" => $_POST['email'],

            "staff" => $_POST['staff'],

        ));
    }

    ?>


</pre>


    <h1>Navigation</h1>

<form action="index.php" method="post" >

    <p>
    <label>firstame: </label>
    <input type="text" name="firstname" size="50" placeholder="Enter First Name" value="">
    </p>

    <p>
    <label>lastname: </label>
    <input type="text" name="lastname" size="50" placeholder="Enter Last Name" value="">
    </P>

    <p>
    <label>date: </label>
    <input type="text" id="datepicker" name="date" size="50" placeholder="Enter First Name" value="">
    </P>

    <p>
    <label>email: </label>
    <input type="text" name="email" " size="50" placeholder="Enter First Name" value="">
    </P>

    <p>
    <label>time: </label>
    <input type="num" id="timer" name="time" size="50" placeholder="Enter First Name" value=""> 
    </p>

    <p>
    <label>staff: </label>
    <input type="text" name="staff" size="50" placeholder="Enter First Name" value="">  
    </p>

    <p>
    <button type="submit" name="submit" value="">Add Apointment</button>
    </p>

    </form>

    <script src="jquery-3.2.1.js"></script>
    <script src="jqueryui/jquery-ui.js"></script>
    <script src="jqueryui/jquery.timepicker.js"></script>
    <script>
            $(document).ready(function() {

            $("#datepicker").datepicker();
            });

                $(document).ready(function() {

                $("#timer").timepicker();

                });

    </script>


</body>
</html>

【问题讨论】:

    标签: php jquery date


    【解决方案1】:

    在此处添加:

    $statement->execute(array(
    
    
            "firstname" => $_POST['firstname'],
    
            "lastname"  => $_POST['lastname'],
    
            "time" => $_POST['time'],
    
            "date" => date('Y-m-d H:i:s', strtotime($_POST['date'])),
    
            "email" => $_POST['email'],
    
            "staff" => $_POST['staff'],
    
        ));
    

    编辑

    其实你应该像这样进行日期转换:

    "date" => DateTime::createFromFormat('m-d-Y', $_POST['date'])->format('Y-m-d H:i:s'),
    

    由于您需要指定转换日期的格式,以确保正确进行转换。

    【讨论】:

    • 谢谢先生,您帮了很多忙。
    • @RyanBaker 请查看我更新的答案以获得正确的转换方法。另外,如果答案对您有用,请点赞/标记为已接受。
    • 你好尝试了新代码,但它没有工作,因为这个错误 - 致命错误:未捕获的错误:调用 C:\xampp\htdocs\usayvisitors\index 中布尔值的成员函数 format() .php:47 堆栈跟踪:#0 {main} 在第 47 行的 C:\xampp\htdocs\usayvisitors\index.php 中抛出
    • 你用的是哪个php版本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    相关资源
    最近更新 更多