SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Needing urgent help with XML parser.

    Hi guys

    OUR XML parser only shows three future events from an XML feed, however it needs to show future events starting TODAY as not tomorrow. Our PHP guy is out and I need to get this altered ASAP, can anyone help alter this code?

    PHP Code:
    <?php
        $testfn 
    'http://website.com';

       function 
    prepareXML($url)
        {
            
    //function to encode and replace certain things that will make an XML file not well formed ie '&' => '&amp;'
            
    $str utf8_encode(str_replace('&''&amp;'file_get_contents($url)));
            
    /*$str = '<?xml version="1.0" encoding="UTF-8"?>' . $str;    */

            
    return $str;
        }

        function 
    loadXML($tempstr
        {
            
    $xmlobject simplexml_load_string($tempstr);

            return 
    $xmlobject;
        }
        
        function 
    getNextEvents($object$numtofetch 3)
        {
            
    //function to recurse through object to find the next x number of events in the future. alter passed value of $numtofetch to change the date range of events.
            
    if (!is_int($numtofetch))
            {
                
    $numtofetch 3;
            }
            
            
    $found = array();
            
    $currentdate time(); 

            foreach(
    $object->Events->Event as $event)
            {
                
    $eventtime strtotime((string) $event->EventStartDateTime);
                if (
    $eventtime >= $currentdate)   !!!!!! not returning a result if event is today !!!!!!
                {
                    
    array_push($found$event);
                }
            }

            
    $found array_reverse($found);
            
    //found now contains all matching events in reverse date order ie next in line to most distant in future
            //trim the array to first 3 and return
            
    return array_slice($found0$numtofetch);
        }

        
    //call function to prepare XML string for passing to SIMPLE XML
        
    $xmlstr prepareXML($testfn);

        
    //call function to load XML using string produced
        
    $xmlObj loadXML($xmlstr);
        
        
    //call to find events in categories...
        
    $events getNextEvents($xmlObj);

    ?>

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    266
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi
    Try replace
    Code:
    $eventtime = strtotime((string) $event->EventStartDateTime);
    With:
    PHP Code:
    $eventtime strtotime($event->EventStartDateTime); 
    And test with
    PHP Code:
    echo $eventtime' >= '$currentdate
    to see what values those data have, maybe there are not selected the rows with timestamp from today, or there is a difference between the date-time of data registered in XML, and servers local time. In this case, a solution can be to decrease the value of $currentdate with one day:
    PHP Code:
    $currentdate strtotime("-1 day");        // or: strtotime("yesterday"); 
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    OR...

    PHP Code:
            $currentdate time(); 
    becomes
    PHP Code:
            $currentdate strtotime('today'); 
    (today returns the time as 00:00:00 on this date.)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your quick replies guys! That should sort it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •