Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8152275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:39:41+00:00 2026-06-06T15:39:41+00:00

This function works fine if I disable errors. But I have to solve them.

  • 0

This function works fine if I disable errors. But I have to solve them.
So I enabled the errors and found the following errors:

Notice: Undefined variable: retval in /home/content/79/9482579/html/gamelinkexchange.com/all/function.php on line 20

Notice: Use of undefined constant length – assumed ‘length’ in /home/content/79/9482579/html/gamelinkexchange.com/all/function.php on line 55

Here is my function:

function time_ago($date,$granularity=2) {
date_default_timezone_set('Asia/Calcutta');
    $date = strtotime($date); 
    $difference = time() - $date;
    $periods = array('decade' => 315360000,
        'year' => 31536000,
        'month' => 2628000,
        'week' => 604800, 
        'day' => 86400,
        'hour' => 3600,
        'minute' => 60,
        'second' => 1);
foreach ($periods as $key => $value) {
        if ($difference >= $value) {
            $time = floor($difference/$value);
            $difference %= $value;
            $retval .= ($retval ? ' ' : '').$time.' '; /*error*/
            $retval .= (($time > 1) ? $key.'s' : $key);
            $granularity--;
        }
        if ($granularity == '0') { break; }
    }
    return $retval.' ago';      
}
function gethours($str)
{
     if(strpos($str, "decade"))
     {
        return 0;
     }
     else if(strpos($str, "year"))
     {
        return 0;
     }
     else if(strpos($str, "month"))
     {
        return 0;
     }
     else if(strpos($str, "week"))
     {
        return 0;
     }
     else if(strpos($str, "day"))
     {
        return 0;
     }
     else if(strpos($str, "hours"))
     {
        $strarr= explode(" ",$str);
        $j=0; /*error*/
        for($i=0;$i<$strarr.length;$i++)
        {
           if($strarr[$i]=="hours")
           { 
                 $j=$i-1;
           }
        }
        return $strarr[$j];
     }
else {   return 1; }
}

This function is running very slow so I need to solve these errors. I tried solution provided in other pages of this website and other forums but nothing did help. Please provide a good solution which consumes less resources.
I need a solution to this. Because every entry has to pass time.

This is my first question, I am a newbie, please help. Thank You.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:39:43+00:00Added an answer on June 6, 2026 at 3:39 pm

    I couldn’t see you initialising your $retval anywhere. You should maybe have something like $retval = '' before you start appending anything to it, based on its value.

    About the other error, you’ve mistaken php with Java. In php you get the length of an array with count($strarr) instead of $strarr.length

    Update
    Instead of

    $retval .= ($retval ? ' ' : '').$time.' ';  
    

    you could simply use

    $retval = $time.' ';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this function: preg_match_all('/((\d{9})\.html)/s', $content, $results); It works fine but i just want
This loop works fine in Main function but when copy this loop code in
I have a function i would like to run onChange. This works fine with
This following code snippet works fine using jQuery1.2.3, but it doesn’t work with latest
I am using this jquery function that works fine in win/mac FF 3.5 and
This works fine: [655971, 2343, 343].map(function(x) { return parseInt(x) }) // [655971, 2343, 343]
This works fine: $.getJSON(http://localhost:59396/xxxWeb/ CarouselHandler.ashx?action=getproducts&ids= + ids, function(data) { carousel.size(allProductIDs.length); if (numberOfImagesLeftToShow < numberOfImagesToDisplay)
This works fine: var vm = {}; $.getJSON('file.json', function (data) { vm.objects = data;
I want to use jquery delegate to call a function, and this works fine:
This are my two functions, on single click it works fine, but on dblclick

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.