Jump to content


Photo

Problem with time before the epoch -- Strtotime function in a search script


  • Please log in to reply
13 replies to this topic

#1 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 30 August 2006 - 02:26 AM

Hi, I'm using a delimiteed text file as a database.  It contains 4 fieds: $day, $month, $year, and $info.  I have a search script that searches the $info field against keywords a user can input then displays the results SORTED by $year.  Everything works fine except for any record that contains a $year value before 1970 (the epoch) it treats it as December 31, 1969 and wrecks my sort order.  Given the following script, what can I do to fix this?

if($_REQUEST['usersearch'] == '1')  // Display Keyword Search Results
{
    $file = file('data.txt');
    $search = $_GET['search'];

    $file = file('data.txt');
    $keys = preg_split('/[\s,]+/', preg_replace('/([^a-z0-9\s])/i', '', trim($search) ) );

    if (sizeof($keys) > 5)
    {
        die("Please enter less keywords.");
    }
    else if (empty($keys[0]))
    {
        die("Please enter a valid keyword.");
    }

    $pattern = '/('. implode('|', $keys) .')/i';
    $matches = array();
    $matchcount = 0;

    foreach (array_values($file) AS $data)
    {
        list($month, $day, $year, $info) = explode('|', trim($data) );

        if (preg_match($pattern, $info))
        {
            $matches[strtotime("$day $month $year")][] = preg_replace($pattern, '<span style="color:red;">$1</span>', $info);
            $matchcount++;
        }
    }

    if (sizeof($matches) > 0)
    {
        ksort($matches);
        $count = 0;

        printf('<center><b><p>%d match%s found.</p></b></center>', $matchcount, $matchcount == 1 ? NULL : 'es');

        foreach ($matches AS $days => $event)
        {
            foreach ($event AS $result)
            {
                list($month, $day, $year) = explode('-', date('F-j-Y', $days) );
                $count++;

                echo '<b><font face="Times New Roman">' . $count . '--' . $month  . '&nbsp;' . $day . ',&nbsp;' . $year . '--' .'</font></b>';
                echo '<font face="Times New Roman" size="2px">' . $result .'</font>';
                echo "<p>";

                if ($count >= 200)
                {
                    break;
                }

            }

            if ($count >= 200)
            {
                break;
            }
        }
        printf('<center><b><p>%d match%s found.</p></b></center>', $matchcount, $matchcount == 1 ? NULL : 'es');
    }
    else
    {
        echo '<p><center><b>No results were found for your search--Please try again.</b></center></p>';
    }

}

Thanks in advance


#2 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 30 August 2006 - 07:09 PM

Is there a few lines of code that might make it recognize dates before 1970?

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 August 2006 - 07:22 PM

try

strtotime ("$year-$month-$day")
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 30 August 2006 - 07:44 PM

That didn't work.  It just changed all my values to two-thousand and something.

This is the line from my above code that I am refering to:
$matches[strtotime("$day $month $year")][] = preg_replace($pattern, '<span style="color:red;">$1</span>', $info);


#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 August 2006 - 08:18 PM

I tried

echo date ('jS F, Y', strtotime ('1949-01-22'));

and got --> 22nd January, 1949
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 30 August 2006 - 08:29 PM

Thanks, I am a newbie and have had help with my code.  Given my code above, what changes would I make to make this work for me?

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 August 2006 - 08:46 PM

What does your data look like?

When you say day, month, year, is that

1, January, 1969
or
01, Jan, 69
or
or 1,1,1969

and how are keywords input?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 30 August 2006 - 08:50 PM

I use F j Y format so todays date would looklike this: August 30, 2006 and the date you mentioned would look this this: January 1, 1969

#9 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 30 August 2006 - 08:55 PM

This is the form where the keyword are inputted

<form action="search.php" method="get">
  <input type="text" name="search" size="20">
  <input type="hidden" name="usersearch" value="1"/>
  <input type="submit" value="Search"/>
</form>


#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 August 2006 - 10:31 PM

It worked as it was with with PHP5 as that supooerts dates pre 1970 but for version 4 I had to convert month names numbers for the sort, then back again for output

<?php
$months = array (
    'January' => '01',
    'February' => '02',
    'March' => '03',
    'April' => '04',
    'May' => '05',
    'June' => '06',
    'July' => '07',
    'August' => '08',
    'September' => '09',
    'October' => '10',
    'November' => '11',
    'December' => '12',
);
if($_REQUEST['usersearch'] == '1')  // Display Keyword Search Results
{
#    $file = file('data.txt');
    $search = $_GET['search'];

    $file = file('data.txt');
    $keys = preg_split('/[\s,]+/', preg_replace('/([^a-z0-9\s])/i', '', trim($search) ) );

    if (sizeof($keys) > 5)
    {
        die("Please enter less keywords.");
    }
    else if (empty($keys[0]))
    {
        die("Please enter a valid keyword.");
    }

    $pattern = '/('. implode('|', $keys) .')/i';
    $matches = array();
    $matchcount = 0;

    foreach (array_values($file) AS $data)
    {
        list($month, $day, $year, $info) = explode('|', trim($data) );

        if (preg_match($pattern, $info))
        {
            $matches["$year {$months[$month]} $day"][] = preg_replace($pattern, '<span style="color:red;">$1</span>', $info);
            $matchcount++;
        }
    }

    if (sizeof($matches) > 0)
    {
        ksort($matches);
        $count = 0;

        printf('<center><b><p>%d match%s found.</p></b></center>', $matchcount, $matchcount == 1 ? NULL : 'es');
        $months = array_flip($months);                              // flip array toconvert months back to names
        foreach ($matches AS $days => $event)
        {
            foreach ($event AS $result)
            {
                list($year, $month, $day ) = explode(' ', $days );
                $month  = $months[$month];                        // convert back to month name
                $count++;

                echo '<b><font face="Times New Roman">' . $count . '--' . $month  . '&nbsp;' . $day . ',&nbsp;' . $year . '--' .'</font></b>';
                echo '<font face="Times New Roman" size="2px">' . $result .'</font>';
                echo "<p>";

                if ($count >= 200)
                {
                    break;
                }

            }

            if ($count >= 200)
            {
                break;
            }
        }
        printf('<center><b><p>%d match%s found.</p></b></center>', $matchcount, $matchcount == 1 ? NULL : 'es');
    }
    else
    {
        echo '<p><center><b>No results were found for your search--Please try again.</b></center></p>';
    }

}
?>
<form action="" method="get">
  <input type="text" name="search" size="20">
  <input type="hidden" name="usersearch" value="1"/>
  <input type="submit" value="Search"/>
</form>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 31 August 2006 - 08:37 PM

The output line:
echo '<b><font face="Times New Roman">' . $count . '--' . $month  . '&nbsp;' . $day . ',&nbsp;' . $year . '--' .'</font></b>';

still shows, December 31, 1969 for all records that have a year value less than 1970.  I am using PHP 4.3.11

Do you think any other lines of code should be changed?  How does changing the months affect the script? 

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 31 August 2006 - 08:45 PM

I've tested the amended script that I posted using v4 and v5 and it works with both; at least it does with this data in "data.txt"

[pre]January|22|1984|Barand 1
January|2|1949|Barand 2
March|1|1980|Barand 3
July|4|2006|Independence Day[/pre]

Entering "ran" as search string gave

[pre]1--January 2, 1949--Barand 2
2--March 1, 1980--Barand 3
3--January 22, 1984--Barand 1[/pre]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#13 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 31 August 2006 - 08:52 PM

Intersting.  Let me double check everything I have snd I will get back with you.

Thanks

#14 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 31 August 2006 - 10:06 PM

Thank you.  It works well.  I did not copy and paste very well - lol.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users