Logical Error?

wbaptist

New Member
Prime Account
Messages
24
Reaction score
1
Points
3
I am working on some code to look at a text file of events and display all events which are current. However, I have a logical error, because a date that is a greater month then the current, less days than the current, and the same year does not display. I suspect the error is in the following if statement. Does anyone see where I messed up? Thanks for your help.

Code:
[COLOR=#000000][COLOR=#007700]if ( [/COLOR][COLOR=#0000bb]$lineDay [/COLOR][COLOR=#007700]< [/COLOR][COLOR=#0000bb]date[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"d"[/COLOR][COLOR=#007700]) && [/COLOR][COLOR=#0000bb]$lineYear [/COLOR][COLOR=#007700]== [/COLOR][COLOR=#dd0000]"20"[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]date[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"y"[/COLOR][COLOR=#007700]) && [/COLOR][COLOR=#0000bb]$lineNumberMonth [/COLOR][COLOR=#007700]== [/COLOR][COLOR=#0000bb]date[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"m"[/COLOR][COLOR=#007700]) )[/COLOR][/COLOR]
PHP:
<?php

$intMonth = date("m"); // number of month
$currentLine = 0; // line counter for file
$currentLineRead = 0;
$writeCurrentLineYN = FALSE;

$file = fopen("dates.txt", "r") or exit("Unable to open file!");


//Output a line of the file until the end is reached
while(!feof($file))
  {
    $currentLineRead = fgets($file);
    $writeCurrentLineYN = proccessLine( " ".$currentLineRead );
    
    if ($writeCurrentLineYN == TRUE )
    {
        echo $currentLineRead."<br />";
        $writeCurrentLineYN = FALSE;
    } // end if

    $currentLine++;
  }

function proccessLine($currentLineProccessing)
{
    $lineNumberMonth = getNumberMonth($currentLineProccessing); // get number month

    
    $lineYear = getYear( $currentLineProccessing); // Determine The Year
    
    $lineDay = getDay( $currentLineProccessing); // determin the day
    
    if ( $lineYear >= "20".date("y") ) // check year
    {
        if ( $lineNumberMonth > date("m") && $lineYear == "20".date("y") ) // check month
        {
            return FALSE;
        } // end if
        else
        {
            if ( $lineDay < date("d") && $lineYear == "20".date("y") && $lineNumberMonth == date("m") )
            {
                return False;
            } // end if
            else
            {
                return TRUE;
            } //end else
        } // end else
    } // end if

} // end function proccessLine

function getDay ($currentLine)
{
    $year = getYear($currentLine);

    $startOfYear = strpos( $currentLine, "$year"); // get string position of year
    
    // get a substring which contains the day
    $subSDay = substr("$currentLine", 1, $startOfYear-1);
    
    $guessDay = 99;
    while (strpos( $subSDay, "$guessDay") == 0)
    {
        $guessDay--;
    } // end while
    return $guessDay;
} // end function getDay

function getYear ($currentLine)
{
    $guessYear = 2000;
    while (strpos( $currentLine, "$guessYear") == 0)
    {    
        $guessYear ++;
    } // end while
    return $guessYear;
} // end function getYear

function getNumberMonth ($currentLine)
{
    // Determine Number Month
    for ($currentLineMonth=1; $currentLineMonth<13; $currentLineMonth++)
    {    
        if ( (strpos( $currentLine, thisMonth($currentLineMonth))) == 1)
        {
            return $currentLineMonth;
        } // end if
    } // end for
} // end function getNumberMonth

function thisMonth($currentMonth)
{
    switch ($currentMonth)
    {
        case 1:
            return "January";
            break;
        case 2:
            return "February";
            break;
        case 3: 
            return "March";
            break;
        case 4:
            return "April";
            break;
        case 5:
            return "May";
            break;
        case 6: 
            return "June";
            break;
        case 7:
            return "July";
            break;
        case 8:
            return "August";
            break;
        case 9:
            return "September";
            break;
        case 10:
            return "November";
            break;
        case 11:
            return "October";
            break;
        case 12:
            return "December";
            break;
    } // end switch
} // end thisMonth

fclose($file);
?>
dates.txt
Code:
June 18, 2003 -- lksjdlkfjsdkljghwe
August 6, 2009 -- lksdjfklasfjdkljds
December 31, 2010 -- sdfjlskdafjlk
July 4, 2023 -- sdfasdfsdf
August 14, 2009 -- sadlkjflasd;j
December 3, 2009 -- sdalfjklkj
December 3, 2010 -- asdlkjfl
October 10, 2009 -- asdlfjlj
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Code:
   if ( $lineNumberMonth > date("m") && $lineYear == "20".date("y") ) 
          return FALSE;
     }

This is August.
date("m") is '08' You are comparing it with a later month '12' (December) .
'12' > '08' and it returns False, which is not what you want.
Edit:
TO DEBUG:

Your function was giving a bad result.
Add debug code to find out which test was failing improperly.


Code:
    if ( $lineYear >= "20".date("y") ) // check year
    {
        if ( $lineNumberMonth > date("m") && $lineYear == "20".dat("y") )        
 {
 
           [B]echo "$currentLineProccessing gave false after first test"[/B] ;
            return FALSE;
        } // end if
        else
        {
            if ( $lineDay < date("d") && $lineYear == "20".date("y") && $lineNumberMonth == date("m") )
            {
 
               [B]echo "$currentLineProccessing gave false after second test" ;[/B]
                return False;
            } // end if
            else
            {
                return TRUE;
            } //end else
        } // end else
    } // end if
 
}

Once you find out where the test is messing up, you can add:

Code:
        if ( $lineNumberMonth > date("m") && $lineYear == "20".dat("y") )        
 {
 
           [B]echo "$currentLineProccessing gave false after first test"[/B] ;
          [B]$XXX = date("m" ) ;
           echo "<br  /> $lineNumberMonth vs $XXX <br />" ;
[/B]
            return FALSE;
        }

To see what the test was looking at.
Edit:
______________________________________

That said, you are going about this the long way.
PHP has a lot of functions to deal with dates and time.

PHP online manual

time() gives you the current time in seconds.
strtotime( TimeString ) turns most TimeStrings into a number representing the time in seconds
You can use a regular expression to grab the date from your file entries.

Quick solution:

Code:
function proccessLine($currentLineProccessing)
{

    $yo = preg_match( '/^.*\d\d\d\d/' , $currentLineProccessing, $match );

    $input_time =  strtotime($match[0] ) ; 
    $t = time();

    if( $input_time > $t ){
          return true;
    }

    return false;


} // end function proccessLine
 
Last edited:
Top