如何使用php搜索CSV文件,方法是检查日期是否在2个范围之间 [英] How to search a CSV file with php by checking if a date falls between 2 ranges

查看:133
本文介绍了如何使用php搜索CSV文件,方法是检查日期是否在2个范围之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个太模糊的上一篇文章。我做了很多研究,并认为我可以更具体。

  while(!feof($ file_handle))
{
$ loansinfo = fgetcsv($ file_handle);
//确保我们只检查游戏的数据
if($ loansinfo [0] == $ ID){
$ referenceDate = $ WantedDate;
$ fromDate =$ loansinfo [5];
$ toDate =$ loansinfo [6];
//将日期转换为时间戳(字符串到整数)
$ referenceTimestamp = strtotime($ referenceDate);
$ fromTimestamp = strtotime($ fromDate);
$ toTimestamp = strtotime($ toDate);
$ isBetween = $ referenceTimestamp> = $ fromTimestamp和$ referenceTimestamp< = $ toTimestamp;
//拒绝订阅
echo('< script type =text / javascript> alert(游戏已预订);< / script>')
exit;
}
}
//否则执行保存代码

是,我总是获得游戏已预订。为什么?



请求的CSV文件数据示例:

  ID,GameName,GameCost,DaysRequested,Total,ReservationStart,DateEnd 
5,Pinball,3.99,7,27.99,01/01/2015,08/01/2015
YYYY-MM-DD c>。我有转换的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 =
}
}
}
}
fclose($ file_handle);

if($ CantBook = true){
echo('< script type =text / javascript> alert(Game is already Booked);< / script& ');
}
else {
//保存预订


I made a previous post that was too vague. I've done a lot of research and think I can be more specific.

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

Problem is, I always get 'Game already booked'. Why?

Sample CSV file data as requested:

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

Though it should be said that the form requires date entry as YYYY-MM-DD. I have java script that does the conversion.

解决方案

I've seen this one! Try this:

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搜索CSV文件,方法是检查日期是否在2个范围之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆