简体   繁体   English

在MySQL表中包含星期几

[英]Including the day of the week in a MySQL table

How would I go about changing the way a 'sign up' date is displayed in a MySQL table? 我如何改变MySQL表中显示“注册”日期的方式? I wanted the date to be formatted in this way: Monday, August 26 2013 6:04PM instead of the usual, standard format based on a 24 hour clock. 我希望以这种方式格式化日期: 2013年8月26日星期一下午6:04而不是基于24小时制的通常的标准格式。

Currently I have: 目前我有:

$value = mysql_real_escape_string($_POST['First']);
$value2 = mysql_real_escape_string($_POST['Last']);
$value3 = mysql_real_escape_string($_POST['City']);
$value4 = mysql_real_escape_string($_POST['State']);
$value5 = mysql_real_escape_string($_POST['Country']);
$value6 = mysql_real_escape_string($_POST['Email']);

SQL: SQL:

$sql = "
  INSERT INTO members
    (First, Last, City, State, Country, Email, Date)
  VALUES
    ('$value', '$value2', '$value3', '$value4', '$value5', '$value6', NOW())
";

I thought changing the format was as simple as this: 我认为改变格式就像这样简单:

$sql = "
  INSERT INTO members
    (First, Last, City, State, Country, Email, Date) 
  VALUES
    ('$value','$value2','$value3','$value4','$value5','$value6' ("l-F-j-Y))
";

Let the date be a date. 让日期为日期。 Just store it the way MySQL wants it to store it and then transform it the way you want it to look like in your presentation layer. 只需按照MySQL希望它存储它的方式存储它,然后按照您希望它在表示层中的样子进行转换。 Even if there isn't any presentation layer just format the date in a select statement using the date formatting functions in MySQL. 即使没有任何表示层,只需使用MySQL中的日期格式化函数在select语句中格式化日期。

Check this link to see those functions. 检查此链接以查看这些功能。

You can refer to the php docs to help you format the date. 您可以参考php文档来帮助您格式化日期。 It will work exactly how you want it. 它将完全按照您的需要工作。

But, I personally prefer to have the unix timestamp in the database, and then have the above function to format it any way I want; 但是,我个人更喜欢在数据库中使用unix时间戳,然后使用上述函数以任何方式格式化它; that allows you to change it from format to format at any time, without having to manually edit every database entry. 允许您随时将其从格式更改为格式,而无需手动编辑每个数据库条目。

NOTE : You should look into PDO or MySQLi because the MySQL functions will be removed in future versions of PHP. 注意 :您应该查看PDOMySQLi,因为在将来的PHP版本中将删除MySQL函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM