日期未保存到数据库

时间:2014-09-25 10:22:08

标签: php mysql

我正在创建一个约会插件,因为已有成员和新成员在那里。在已经成员区域中,日期条目不会进入db。但是当显示变量包含日期时。我从早上开始检查这个,但没有得到错误。我的代码是:

 $source = mysql_real_escape_string(trim($_POST['apdatetime']));
$datetime = explode(',', $source);
$dates = $datetime[0];
  $app_time = $datetime[1];
if($app_time < 12){
   $app_session = 'am';
  }
 else{
    $app_session ='pm';
   }
$splitdatet = explode('/', $dates);
$yyear = $splitdatet[2];
$mmonth = $splitdatet[1];

$ddate = $splitdatet[0];
$app_date = $yyear . "-" . $mmonth . "-" . $ddate;


if ($_POST['isnewpatient'] == "false") {


        //$cSql = "select * from " . WP_contact . " where appointments_c_patientid='" . trim($_POST['ptntid']) . "' ";
        $cSql = "select * from " . WP_eemail_TABLE_SUB . " where eemail_patient_id='" . trim($_POST['ptntid']) . "' ";

        $data = $wpdb->get_results($cSql);
        if (empty($data )) {
            $err = 1;
            echo "<div id='message' class='aerror'>No such patient ID exists....</div>";
        } else {

            @$mobile = htmlspecialchars(stripslashes($data[0]->eemail_mobile_sub));
            @$email = htmlspecialchars(stripslashes($data[0]->eemail_email_sub));
            @$name = htmlspecialchars(stripslashes($data[0]->eemail_name_sub));
           $sqlss = "insert into " . WP_Appointments .
                    " (`appointments_patient_id`,`appointments_date`,`appointments_time`,`appointments_session`,`appointments_reg_date`) VALUES ('" .
                    mysql_real_escape_string(trim($_POST['ptntid'])) . "','" .
                    $app_date . "','" .
                    $app_time . "','" .
                    $app_session . "',CURRENT_TIMESTAMP() )";
      $dd=$wpdb->get_results($sqlss);
var_dump($dd);
echo 'Date:'.$app_date;
            // return $suc;
            echo "<div id='message' class='asuccess' >Request has been sent for appointment</div>";
        }
    }

var_dump的输出为array[0],$ app_date为Date:2014-10-22 db条目是

appointments_id           :393
appointments_patient_id   : 9999999999
appointments_date         : 0000-00-00
appointments_time         : 9:00
appointments_session      : am
appointments_reg_date     : 2014-09-25 14:21:35

有人可以指出代码中的错误吗?

1 个答案:

答案 0 :(得分:0)

您应该将字符串转换为日期对象,如下所示:

$app_date = date('Y-m-d',strtotime($app_date));