Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,108
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Last Activity Function

    I could use a second set of eyes in determining if a Function I wrote has enough "Error-Handling" in it?!

    Here is my code...
    PHP Code:
        function getOnlineStatus($lastActivity){

    //HOW DO I DO ERROR-HANDLING FOR THIS??

            /**
             * Returns Member's Online Status
             *
             * Takes User's "Last Activity", and determines the User's Online Status.
             * Returns HTML for appropriate Online Status Indicator.
             *
             * @param        DateTime    $lastActivity                                    MySQL DateTime format (yyyy-mm-dd hh:mm:ss)
             * @return    Array            {Image Filename, Alt Text}
             */

            // time() is current time measured in the # of seconds since Unix Epoch (January 1 1970 00:00:00 GMT).
            // strtotime() converts MySQL DateTime format (yyyy-mm-dd hh:mm:ss) into a Unix Timestamp (seconds).
            
    $minutesOnline = (time() - strtotime($lastActivity))/60;

            
    // Determine Online Status.
            
    if ($minutesOnline 15){
                
    // Member Online
                
    $indicator = array( "Light_Green_10.png","Member Online");

            }else if (
    $minutesOnline 30){
                
    // Member Idle
                
    $indicator = array("Light_Yellow_10.png","Member Idle");

            }else{
                
    // Member Offline
                
    $indicator = array("Light_Gray_10.png" ,"Member Offline");

            }

            return 
    $indicator;

        }
    //End of getOnlineStatus 

    Not sure if I need to be checking for a Null, String, or Zero for $lastActivity??


    (BTW, I am sure I could write a better Function as far as separating Logic from Presentation, but for now this will do!)

    Thanks,


    Debbie

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, this isn't correct. For error handling, the strtotime is a tricky beast.
    strtotime will return false as of 5.1.0, and prior it will return -1. -1 is not ideal since it is technically a valid timestamp, so you need to check that its return time and compare it to the version before continuing.
    PHP Code:
    $timestamp strtotime($lastActivity);
    if (
    $timestamp !== false)
    {
        
    // so far its okay
        
    if ($timestamp == -&& version_compare(PHP_VERSION'5.1.0') < 0)
        {
            
    // this one is no good, so respond accordingly.
        
    }
        else
        {
            
    // all conditions are good
        
    }
    }
    else
    {
        
    // no good

    Instead you could use the DateTime class which you can try/catch. Although if you don't care about a specific version of PHP, then strtotime is fine as well and just check it for false.

    Move the doc comment before the function signature otherwise it won't be a part of the reflection. Also don't call the parameter DateTime as that would indicate it is a DateTime object and an IDE may interpret it as such. Call it as it is, a string.

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,108
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Fou-Lu,

    Thanks for the tips!


    Debbie


  •  

    Posting Permissions

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