将日期插入mysql但存储当前日期

时间:2017-06-17 08:16:51

标签: php android

我有一个Android应用程序,我使用PHP编写Web服务,我的数据库是MYSQL数据库。在android中有一个表单来收集有关使用的信息,并使用我的php服务将其保存到数据库中。

filed_name:entry_date type:varchar(50)

entry_date是用户可以从UI进入的。每当我将数据插入数据库时​​,输入日期值就是今天的日期。

如果我使用默认值单独运行php,它将正常工作。当我从app运行它没有输入正确的日期。

$entry_date = $_REQUEST['entry_date'];
if(isset($entry_date))
{
    $x = explode("-", $entry_date);
    $entry_date = $x[2]."-".$x[1]."-".$x[0];
    $entry_date = strtotime($entry_date);
    $entry_date = date('Y-m-d', $entry_date);
}

在android中,我像这样传递

values.add(new BasicNameValuePair("entry_date", entry_date_text));

2 个答案:

答案 0 :(得分:0)

你应该使用strtotime()http://php.net/manual/en/function.strtotime.php

// You don't need to contain a array element into variable
// $entry_date = $_REQUEST['entry_date'];
if(isset($_REQUEST['entry_date'])) {
    $entry_date = strtotime( $_REQUEST['entry_date] );
}

答案 1 :(得分:0)

像这样使用,这对我有用

<?php
    $entry_date = "10-05-2017";
    if(isset($entry_date))
    {
        $x = explode("-", $entry_date);
        $entry_date = $x[2]."-".$x[1]."-".$x[0];
        $entry_date = date('Y-m-d', strtotime($entry_date));
        echo $entry_date;
    }
?>