gpt4 book ai didi

PHP - 数组的 else 参数不起作用

转载 作者:行者123 更新时间:2023-11-29 12:50:41 24 4
gpt4 key购买 nike

当前数组中有一个从 Date1 到 Date2 的日期列表,然后我尝试将 mysql 数据库中的日期放入数组中。从那里开始,数据库中以及 date1 和 date2 之间存在的日期应该显示为“已采取”,因为不会采取数据库中没有的日期。到目前为止的代码是:

<?php
date_default_timezone_set('America/Chicago');
$con = mysqli_connect("localhost", "apple", "Orange1", "cal");
$date1 = new DateTime('2014-06-10'); // From date: 10/06/2014
$date2 = new DateTime('2014-06-30'); // To date: 30/06/2014
$datesTaken = array();
$result = mysqli_query($con, "SELECT `events` FROM `events`"); //get dates from events table

while ($row = mysqli_fetch_array($result)) { //iterate over each row in SQL
$datesTaken[] = new DateTime($row['date1']); //add taken dates into array //add dates to the $datesTaken array.
}

while ($date1 <= $date2) { //loop until dates are equal
if (in_array($date1, $datesTaken)) { //if the date is in the datesTaken array it is taken
echo $date1->format("Y-m-d") . " taken. <br />"; //Dates which are taken
}
else {
echo $date1->format("Y-m-d") . " not taken. <br />"; //Dates which aren't taken
}

date_add($date1, date_interval_create_from_date_string('1 day')); //adding a day to $day1 then looping through again unless $date1 is greater
}

?>

目前代码的结果是:

2014-06-10 not taken.
2014-06-11 not taken.
2014-06-12 not taken.
2014-06-13 not taken.
2014-06-14 not taken.
2014-06-15 not taken.
2014-06-16 not taken.
2014-06-17 not taken.
2014-06-18 not taken.
2014-06-19 not taken.
2014-06-20 not taken.
2014-06-21 not taken.
2014-06-22 not taken.
2014-06-23 not taken.
2014-06-24 not taken.
2014-06-25 not taken.
2014-06-26 not taken.
2014-06-27 not taken.
2014-06-28 not taken.
2014-06-29 not taken.
2014-06-30 not taken.

当前数据库中具有相同格式的两个日期是:“2014-06-14”和“2014-06-15”我尝试完全删除 mysqli 查询,只是简单地删除

$datesTaken = array("2014-06-14","2014-06-15");

结果还是一样。对编程非常陌生,非常感谢任何帮助,谢谢。

最佳答案

您的查询是错误的,因为我不确定列名,可能是以下任一可能性。

SELECT `events` as date1 FROM `events`

SELECT `date1` FROM `events`

关于PHP - 数组的 else 参数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24786368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com