下面的PHP代码在当前日期工作正常,可以从MySQL获取数据,以便参与定义的特定ID。我需要它来获取当前的一周和一个月。我用Google搜索了很多但无法解决的问题。小帮助将受到很多赞赏。
如果有可能使用JavaScript的方式,请告诉我。
// SELECT Time Zone
date_default_timezone_set("Europe/Belfast");
// SELECT Date With Default Time i.e "00:00:00"
$date= date("y-m-d 00:00:00");
$mysqli=mysqli_connect('localhost','Uid','PASS','DB');
//Passed From Ajax
$Fetch_date = $_POST['ID'];
// Example Date is 2015-06-18 00:00:00 And id is 12345
$query ="SELECT * FROM attendance WHERE RegID='$Fetch_date' And Date='$date' ";
$result = mysqli_query($mysqli,$query)or die(mysqli_error());
$num_row = mysqli_num_rows($result);
while($row=mysqli_fetch_array($result))
{
//Echo result hear Working Fine
}
答案 0 :(得分:2)
本月:
$date1 = date('Y-m-01 00:00:00');
$date2 = date('Y-m-d 23:59:59', strtotime('last day of this month'));
$sql = "select * from attendance where RegID='$Fetch_date' and Date > '$date1' and Date < '$date2' ";
本周:
$date1 = date('Y-m-d 00:00:00', strtotime('monday this week'));
$date2 = date('Y-m-d 23:59:59', strtotime('sunday this week'));
$sql = "select * from attendance where RegID='$Fetch_date' and Date > '$date1' and Date < '$date2' ";
P.S。:这个问题与javascript无关。
答案 1 :(得分:2)
您可以使用mysql-date-functions:
$query ="SELECT * FROM attendance
WHERE RegID = '$Fetch_date'
AND YEAR(Date) = YEAR(NOW())
AND MONTH(Date) = MONTH(NOW())
AND WEEKOFYEAR(Date) = WEEKOFYEAR(NOW())";
在Javascript中你可以用类似的方式测试它,
使用getFullYear
和getMonth
。返回一年中的周的函数不可用,但可以使用基于Math.floor(MillisecondsOfTheDate/MillisecondsOfAWeek)
的比较