Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-23-2012, 10:29 PM   PM User | #1
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
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
doubledee is offline   Reply With Quote
Old 11-23-2012, 11:23 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-24-2012, 09:59 PM   PM User | #3
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Fou-Lu,

Thanks for the tips!


Debbie
doubledee is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:38 AM.


Advertisement
Log in to turn off these ads.