mysql 日期格式 dd/mm/yyyy?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12003205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql date format dd/mm/yyyy?
提问by Patrick
Does anyone know how to insert into column(of date type) date in the format of dd-mm-yyyy?
有谁知道如何以 dd-mm-yyyy 格式插入(日期类型)列中的日期?
I'm using calendar, and it selects days like dd-mm-yyyy, but it can't be inserted because of the format that uses mysql by default (yyyy-mm-dd).
我正在使用日历,它选择了像dd-mm-yyyy这样的日子,但由于默认使用mysql的格式(yyyy-mm-dd)无法插入。
The problem is, that I need to compare dates that has been inserted by the calendar and the dates that has been automatically inserted with current_date function. Basicly I have column DoR (date_of_registration) and Status. I've made DoR of date type, and Status of varchar(). In the Status I use Calendar to insert the date. Thus, I've compared it anyway but it gives strange results. I guess it is not comparing correctly.
问题是,我需要比较日历插入的日期和使用 current_date 函数自动插入的日期。基本上我有列 DoR (date_of_registration) 和 Status。我已经制作了日期类型的 DoR 和 varchar() 的状态。在状态中,我使用日历插入日期。因此,无论如何我都比较过它,但它给出了奇怪的结果。我想它没有正确比较。
Any ideas?
有任何想法吗?
Thanks
谢谢
回答by Bhuvan Rikka
Convert the output from calendar into date format like this
将日历的输出转换为这样的日期格式
$cal_date=17-08-2012;
$date=date('Y-m-d',strtotime($cal_date));
echo $date; //gives date as 2012-08-17
Now you can insert this date into mysql
and do comparisons
现在您可以将此日期插入mysql
并进行比较
回答by juergen d
回答by fancyPants
All you need are these two functions:
你只需要这两个函数:
and
和
回答by Konerak
Please, always store dates as a DATE type in MySQL. If you need to select it in another form, use the correct string function DATE_FORMAT!
请始终将日期存储为 MySQL 中的 DATE 类型。如果您需要以其他形式选择它,请使用正确的字符串函数DATE_FORMAT!
回答by Konerak
You don't need to insert database as dd-mm-yyyy. You have to user date format. If you are writing code in php you can parse date what every type you want
您不需要将数据库插入为 dd-mm-yyyy。您必须使用日期格式。如果您在 php 中编写代码,您可以解析您想要的每种类型的日期
Select datetime for database. And insert date data date("Y-m-d H:i:s");
when you want to echo date different formats like
选择数据库的日期时间。并date("Y-m-d H:i:s");
在您想要回显日期不同的格式时插入日期数据,例如
echo date('d-m-Y', strtotime('2012-08-17 19:00:00'));
回答by Bhatt Akshay
There is also another way to store the date like, if you are choose that
还有另一种存储日期的方法,如果您选择
`<input type="date" name="bdate" />
`
then it directly store the date in to database in yyyy-MM-DD Format. Here my text field format is dd-mm-yyyy. Here you don't have to convert the string using
strtotime()
然后它直接将日期以 yyyy-MM-DD 格式存储到数据库中。这里我的文本字段格式是 dd-mm-yyyy。在这里您不必使用转换字符串
strtotime()