将 PHP 日期插入 MySQL 数据库 [英] Insert PHP date into MySQL database

查看:119
本文介绍了将 PHP 日期插入 MySQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将表单中的日期字段插入到 mysql 数据库中.如果用户未输入日期,则应使用当前日期.我的代码是:

I need to insert a date field from a form into a mysql database. If the user does not input a date, the current date should be used. My code is :

if ($_POST[date]==0)
    {$ref_date=date('Y-d-m');}
    else {$ref_date=$_POST[date];}

$L="insert into referral (ID,patient, Doctor,Date) values (default,$pat,$doc,'$ref_date')";
        mysql_query($L) or die(mysql_error());

当我在表单中提交日期时它工作正常,但是当我将该字段留空时,它会输入 0000-00-00.

It works fine when I submit a date in the form, but when I leave that field blank it inputs 0000-00-00.

推荐答案

MySQL 希望日期采用 YYYY-MM-DD 格式.你有 YYYY-DD-MM.更改:

MySQL expects dates to be in YYYY-MM-DD format. You have YYYY-DD-MM. Change:

$ref_date=date('Y-d-m');

到:

$ref_date=date('Y-m-d');

这篇关于将 PHP 日期插入 MySQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆