我的代码的想法是获取表格中所有在特定月份和年份发生的更新行,例如更新发生在:2012年5月。 因为我有2个显示月份和年份的下拉菜单,我想在选择月份和年份时,将显示在所选月份和年份中发生的所有更新行。 有没有人可以帮我解决这个问题的想法,我会很高兴。 提示:数据库中存储的日期格式为(Y-m-d)。
答案 0 :(得分:0)
提示:不要给我们提示,但要讲述整个故事。
您的问题的可能解决方案(适用于php 5.3 +):
<?php
$startDate = date("Y-m-d", strtotime("first day of the month", strtotime( (int) $_POST['year']."-".(int)$_POST['month'].-"01 00:00")));
$endDate = date("Y-m-d", strtotime("last day of the month", strtotime( (int) $_POST['year']."-".(int)$_POST['month'].-"01 23:59")));
$db = ; //your PDO connection
$q = $db->prepare("SELECT `thingstoselect` FROM `yourtable` WHERE `date` > ? && `date` < ?");
$q->execute( array( $startDate, $endDate));
$yourResults = $q->fetchAll();
答案 1 :(得分:0)
您是将其存储为varchar还是日期?
如果您将其存储为日期列,则可以将$ year和$ month变量与以下内容进行比较:
select * from TABLE where year(datecol) > $year or (year(datecol) = $year and month(datecol) >= $month);
如果它是一个子字符串,你必须为大于的字符串进行一些有趣的转换,但你可以使用子字符串来查找完全匹配:
select * from TABLE where substring_index(stringcol,'-',1) > $year or ( substring_index(stringcol,'-',1) = $year and substring_index(substring_index(stringcol,'-',2),'-',-1) >= $month );
问题是MySQL没有内置的字符串拆分功能。