+ Reply to Thread
Results 1 to 3 of 3

Thread: Logical Error?

  1. #1
    wbaptist is offline x10Hosting Member wbaptist is an unknown quantity at this point
    Join Date
    Jan 2009
    Posts
    21

    Logical Error?

    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:
    if ( $lineDay < date("d") && $lineYear == "20".date("y") && $lineNumberMonth == date("m") )
    PHP Code:
    <?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$currentLinethisMonth($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

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Logical Error?

    Quote Originally Posted by wbaptist View Post

    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.


    Quote Originally Posted by wbaptist View Post
    Code:
     
        if ( $lineYear >= "20".date("y") ) // check year
        {
            if ( $lineNumberMonth > date("m") && $lineYear == "20".dat("y") )        
     {
     
               echo "$currentLineProccessing gave false after first test" ;
                return FALSE;
            } // end if
            else
            {
                if ( $lineDay < date("d") && $lineYear == "20".date("y") && $lineNumberMonth == date("m") )
                {
     
                   echo "$currentLineProccessing gave false after second test" ;
                    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") )        
     {
     
               echo "$currentLineProccessing gave false after first test" ;
              $XXX = date("m" ) ;
               echo "<br  /> $lineNumberMonth vs $XXX <br />" ;
    
                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 by descalzo; 08-14-2009 at 06:21 PM. Reason: Automerged Doublepost
    Nothing is always absolutely so.

  3. #3
    wbaptist is offline x10Hosting Member wbaptist is an unknown quantity at this point
    Join Date
    Jan 2009
    Posts
    21

    Re: Logical Error?

    Thank for the help that worked!

+ Reply to Thread

Similar Threads

  1. Replies: 2
    Last Post: 03-18-2009, 03:19 PM
  2. 500 Internal Server Error on cossacks
    By tletter in forum Free Hosting
    Replies: 1
    Last Post: 12-03-2008, 07:27 PM
  3. error 500
    By robertofrancos in forum Soporte
    Replies: 10
    Last Post: 12-05-2007, 10:42 AM
  4. Error en mi host
    By roonline in forum Soporte
    Replies: 17
    Last Post: 11-14-2007, 03:28 PM
  5. 500 Internal Server Error (PHP Error?)
    By aniklaus in forum Free Hosting
    Replies: 3
    Last Post: 11-12-2007, 11:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers