简体   繁体   English

PHP MySQL的日期/时间问题

[英]php mysql date/time question

I've built a small application which has User Management, a frontend console to enter data and a backend console to control parts of the frontend. 我构建了一个小型应用程序,它具有用户管理,一个用于输入数据的前端控制台和一个用于控制前端部分的后端控制台。 The frontend adds rows to a MySQL database which are timestamped. 前端将带有时间戳的行添加到MySQL数据库。 The backend needs to be able to select rows from the database between X and Y dates. 后端需要能够在X和Y日期之间从数据库中选择行。

Everything works so far, except the date part which I'm really struggling with. 到目前为止,所有内容都可以正常运行,除了我真正在挣扎的日期部分。

The frontend SQL input looks like this (simplified with spurious code removed): 前端SQL输入看起来像这样(简化为删除了伪造的代码):

$date = time();
$top_level_category = $_POST['top_level_category'];
$sub_level_category = $_POST['sub_level_category'];
$company = $_POST['company'];
$agent_name = $_POST['agent_name'];
$ticket_id = $_POST['ticket_id'];

$sql = "INSERT INTO dacc_data ("
     .     "id, top_level_category, sub_level_category, "
     .     "agent_name, date, ticket_id, company"
     . ") VALUES ("
     .     "NULL, '$top_level_category', '$sub_level_category', "
     .     "'$agent_name', FROM_UNIXTIME('$date'), '$ticket_id', '$company'"
     . ")"
;

$result = mysql_query($sql) or die (mysql_error());  

That seems to work ok, the timestamp is being picked up and added to a DATETIME column in my table. 这似乎正常,已将时间戳记添加到表中的DATETIME列中。 It displays as dd/mm/yyyy hh:mm:ss within the database. 它在数据库中显示为dd / mm / yyyy hh:mm:ss。

So ... my first question is - is this the right way to do it? 所以...我的第一个问题是-这是正确的方法吗?

The second question being, what sort of SQL statement would I need to pull out an array of rows between X and Y date. 第二个问题是,我需要哪种类型的SQL语句才能提取X和Y日期之间的行数组。

Apologies if this is rambling a bit, hope it's clear but if you need more information let me know. 抱歉,这有点麻烦,希望这很清楚,但是如果您需要更多信息,请告诉我。

MySQL datetime should be formatted with dashes: MySQL日期时间应使用破折号格式:

YYYY-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS

http://dev.mysql.com/doc/refman/5.0/en/datetime.html http://dev.mysql.com/doc/refman/5.0/en/datetime.html

Then you can query for date ranges a couple of ways: 然后,您可以通过以下两种方式查询日期范围:

select *
from table
where date >= '[start date]' and date <= '[end date]';

or 要么

select * 
from table 
where date between '[start date]' and '[end date]';

where "table" is the name of your database table and "date" is the name of your datetime field. 其中“表”是数据库表的名称,“日期”是您的日期时间字段的名称。

You are correct. 你是对的。 I can confirm that the Database has "YYYY-MM-DD HH:MM:SS" - I am using SQLWave editor for browsing the DB quickly, it auto-formats the DATETIME column. 我可以确认数据库具有“ YYYY-MM-DD HH:MM:SS”-我正在使用SQLWave编辑器快速浏览数据库,它会自动格式化DATETIME列。

// Initial questions still stand :)

Or not, just noticed you updated the answer - thank you very much! 还是没有,只是注意到您更新了答案-非常感谢! I had actually tried that very same query several times to no avail, mainly because my WHERE was specifying the date format incorrectly. 实际上,我几次尝试过同样的查询都无济于事,主要是因为我的WHERE错误地指定了日期格式。 Misled by SQLWave :( 被SQLWave误导了:(

Back to using command line from now on !!! 从现在开始返回使用命令行!

“我需要哪种类型的SQL语句来提取X和Y日期之间的行数组?”

SELECT * FROM `dacc_data` where `date` between "2008-11-01" and "2008-12-01"

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

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