简体   繁体   English

使用PHP使用两个日期字段和当前月份选择表条目

[英]Using PHP to select table entries using two date fields and current month

Please help a newbee. 请帮助新手。 I have a table named event_calendar with fields named ec_start_date, ec_end_date and ec_event_name. 我有一个名为event_calendar的表,其中的字段名为ec_start_date,ec_end_date和ec_event_name。 The first two fields are date fields. 前两个字段是日期字段。 It is a small table, with less than 100 entries. 这是一个小表,少于100个条目。 I want to list events with a start or end date in the current month, then I want to follow with a list of events with a start or end date in the next month. 我想列出当前月份中具有开始或结束日期的事件,然后我要列出下个月中具有开始或结束日期的事件的列表。 The two list will be headed by the month name. 这两个列表将以月份名称开头。 I would like the dates displayed in the list to be in the format dd/mm. 我希望列表中显示的日期为dd / mm格式。

This is the code I've found to identify the months for the list headers. 这是我发现的用于识别列表标题月份的代码。

$thismonth = mktime(0,0,0,date("n"));
$nextmonth = mktime(0,0,0,date("n")+1);

echo "<h2 class='caps'>";
echo date("n", $thismonth);
echo "</h2>";

echo "<h2 class='caps'>";
echo date("m", $nextmonth);
echo "</h2>";

This is the code I use to pull the entries for this month's (August) activities 这是我用来提取本月(八月)活动条目的代码

$query = "SELECT ec_display,ec_event_name,ec_start_date,ec_end_date FROM event_calendar WHERE month(ec_start_date) = 8 OR month(ec_end_date) = 8 ORDER BY ec_start_date";

The problem is, if I replace the number 8 with the variable $thismonth, it fails. 问题是,如果我用变量$ thismonth替换数字8,它将失败。

Finally, how can I display only the dd/mm from ec_start_date and ec_end_date? 最后,如何仅显示ec_start_date和ec_end_date中的dd / mm?

I greatly appreciate any guidance, but please be specific as I am very new to this! 我非常感谢您提供任何指导,但是请具体说明,因为我对此还很陌生! Thank you! 谢谢!

$thismonth contains a UNIX timestamp returned by mktime . $thismonth包含mktime返回的UNIX时间戳。 The timestamp for hour 0, minute 0, second 0 of month 8 of this year is 1312862400. That's not 8. 今年8月份的小时0,分钟0,秒0的时间戳是1312862400。不是8。

Don't put that in your query, put date('n') (8) in it... or just let MySQL do it 不要在查询中放入date('n') (8)...或者让MySQL执行

$query = "SELECT ec_display,ec_event_name,ec_start_date,ec_end_date FROM event_calendar WHERE month(ec_start_date) = MONTH(CURRENT_DATE) OR month(ec_end_date) = MONTH(CURRENT_DATE) ORDER BY ec_start_date";

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

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