php - 如何使用 php 通过检查日期是否在 2 个范围之间来搜索 CSV 文件

标签 php csv

我之前发过一篇文章,内容太模糊了。我做了很多研究,认为我可以更具体。

while (!feof($file_handle)) 
{
    $loansinfo = fgetcsv($file_handle);
    // Make sure we only check data for the game we posted
    if($loansinfo[0]==$ID) { 
        $referenceDate = $WantedDate;
        $fromDate = "$loansinfo[5]";
        $toDate = "$loansinfo[6]";
        // Convert dates to timestamps (strings to integers)
        $referenceTimestamp = strtotime( $referenceDate );
        $fromTimestamp = strtotime( $fromDate );
        $toTimestamp = strtotime( $toDate );
        $isBetween = $referenceTimestamp >= $fromTimestamp and $referenceTimestamp <= $toTimestamp;
        //refuse booking    
        echo('<script type="text/javascript">alert("Game Already Booked");</script>');
        exit;
    }
}
// otherwise execute save code

问题是,我总是收到“游戏已预订”。为什么?

根据要求提供示例 CSV 文件数据:

ID, GameName,GameCost, DaysRequested, Total, ReservationStart, DateEnd
5,Pinball, 3.99,7, 27.99, 01/01/2015, 08/01/2015

尽管应该说该表单需要输入日期为YYYY-MM-DD。我有执行转换的 java 脚本。

最佳答案

这个我见过!试试这个:

while (!feof($file_handle)) {
        $loansinfo = fgetcsv($file_handle);
        if($loansinfo[0]==$ID){
                $FromDate = "$loansinfo[5]";
                $ToDate ="$loansinfo[6]";
                if (strtotime($DateBorrowedFrom) <= strtotime($WantedDate)) {
                    if(strtotime($ToDate) >= strtotime($WantedDate)){
                        $CantBook = True;
                        }
                }
                else {
                    if (strtotime($DateBorrowedFrom)  <= strtotime($DateTo)) {
                        $CantBook= true;
                        }
                    }
                }
            }
        fclose($file_handle);

        if($CantBook = true){
            echo('<script type="text/javascript">alert("Game is already Booked");</script>');
            }
        else{
    //Saving the booking

关于php - 如何使用 php 通过检查日期是否在 2 个范围之间来搜索 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30121721/

相关文章:

php - 用于大量 MySql 插入和更新的 RabbitMQ?

php - 如何从 PHP 重新启动 memcached

php - 单元测试应该遵循 PSR 吗?

php - Laravel 4 资源 Controller NotFoundHttpException

Python 求和并组织 csv 数据

python - 使用 NumPy asarray 方法将列表转换为数组

javascript - 使用 Javascript 从本地文件夹读取 CSV 文件

php - Codeigniter 3 删除 index.php 问题

ruby - 如何在 ruby​​ 中读取带有奇怪引用的 CSV?

java - 带有 csv 输入文件的 jMeter maven 插件