Results 1 to 3 of 3

Thread: [RESOLVED] Trying to add time to function

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Resolved [RESOLVED] Trying to add time to function

    I found a function that will find how long ago a date occurred:

    Code:
    function get_time_ago($time_stamp)
    {
        $time_difference = strtotime("now") - $time_stamp;
    
        if ($time_difference >= 60 * 60 * 24 * 365.242199)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 365.242199 days/year
             * This means that the time difference is 1 year or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24 * 365.242199, 'year');
        }
        elseif ($time_difference >= 60 * 60 * 24 * 30.4368499)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 30.4368499 days/month
             * This means that the time difference is 1 month or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24 * 30.4368499, 'month');
        }
        elseif ($time_difference >= 60 * 60 * 24 * 7)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 7 days/week
             * This means that the time difference is 1 week or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24 * 7, 'week');
        }
        elseif ($time_difference >= 60 * 60 * 24)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day
             * This means that the time difference is 1 day or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24, 'day');
        }
        elseif ($time_difference >= 60 * 60)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour
             * This means that the time difference is 1 hour or more
             */
            return get_time_ago_string($time_stamp, 60 * 60, 'hour');
        }
        else
        {
            /*
             * 60 seconds/minute
             * This means that the time difference is a matter of minutes
             */
            return get_time_ago_string($time_stamp, 60, 'minute');
        }
    }
    
    function get_time_ago_string($time_stamp, $divisor, $time_unit)
    {
        $time_difference = strtotime("now") - $time_stamp;
        $time_units      = floor($time_difference / $divisor);
    
        settype($time_units, 'string');
    
        if ($time_units === '0')
        {
            return 'less than 1 ' . $time_unit . ' ago';
        }
        elseif ($time_units === '1')
        {
            return '1 ' . $time_unit . ' ago';
        }
        else
        {
            /*
             * More than "1" $time_unit. This is the "plural" message.
             */
            // TODO: This pluralizes the time unit, which is done by adding "s" at the end; this will not work for i18n!
            return $time_units . ' ' . $time_unit  . 's ago';
        }
    }
    ?>
    The function works great. However it's based on the location the server which is in a different time zone.

    I have a field in mysql db that stores the time difference and I want to add the time difference to the function so events that are displaying as "xx hours ago" are displaying relative to the users timezone.

    Here's what I was working on:

    Code:
    ...
    ...
    
    mysql_select_db($database_connectMichels, $connectMichels);
    $query_adjusttime = sprintf("SELECT Time_From_Server FROM companies WHERE Company_ID =", GetSQLValueString($colname_adjusttime, "int"));
    $adjusttime = mysql_query($query_adjusttime, $connect) or die(mysql_error());
    $row_adjusttime = mysql_fetch_assoc($adjusttime);
    $totalRows_adjusttime = mysql_num_rows($adjusttime);
    
    function get_time_ago($time_stamp)
    {
    	$add = $row_adjusttime['Time_From_Server'];
        $time_difference = strtotime("now $add") - $time_stamp;
    
        if ($time_difference >= 60 * 60 * 24 * 365.242199)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 365.242199 days/year
             * This means that the time difference is 1 year or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24 * 365.242199, 'year');
        }
        elseif ($time_difference >= 60 * 60 * 24 * 30.4368499)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 30.4368499 days/month
             * This means that the time difference is 1 month or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24 * 30.4368499, 'month');
        }
        elseif ($time_difference >= 60 * 60 * 24 * 7)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 7 days/week
             * This means that the time difference is 1 week or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24 * 7, 'week');
        }
        elseif ($time_difference >= 60 * 60 * 24)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day
             * This means that the time difference is 1 day or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24, 'day');
        }
        elseif ($time_difference >= 60 * 60)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour
             * This means that the time difference is 1 hour or more
             */
            return get_time_ago_string($time_stamp, 60 * 60, 'hour');
        }
        else
        {
            /*
             * 60 seconds/minute
             * This means that the time difference is a matter of minutes
             */
            return get_time_ago_string($time_stamp, 60, 'minute');
        }
    }
    
    function get_time_ago_string($time_stamp, $divisor, $time_unit)
    {
    	$add = $row_adjusttime['Time_From_Server'];
        $time_difference = strtotime("now $add") - $time_stamp;
        $time_units      = floor($time_difference / $divisor);
    
        settype($time_units, 'string');
    
        if ($time_units === '0')
        {
            return 'less than 1 ' . $time_unit . ' ago';
        }
        elseif ($time_units === '1')
        {
            return '1 ' . $time_unit . ' ago';
        }
        else
        {
            /*
             * More than "1" $time_unit. This is the "plural" message.
             */
            // TODO: This pluralizes the time unit, which is done by adding "s" at the end; this will not work for i18n!
            return $time_units . ' ' . $time_unit  . 's ago';
        }
    }
    ?>
    <?php
    mysql_free_result($company);
    ?>
    The php code queries my db for the dynamic field $row_adjusttime['Time_From_Server']. I am trying to add this to the function at two spots. An example of what the variable might be equal to in my db...

    $row_adjusttime['Time_From_Server'] = '+2 hours'

    I thought if I store it like this, then I could just modify the code to:

    Code:
    $add = $row_adjusttime['Time_From_Server'];
    $time_difference = strtotime("now $add") - $time_stamp;
    But this doesn't work at all. The function ignores it and the time is still based on server location.

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Trying to add time to function

    Use date_default_timezone_set(). Set it to the timezone you want.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Trying to add time to function

    Well, that was easy!

    Thanks a ton.

Posting Permissions

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



Click Here to Expand Forum to Full Width