0

I have 2 functions first to detect language and other to convert time into relative time

index.php Is the file where i am calling the 2 functions with include_once

include_once('./backend/detect_language.php');
include_once('./backend/relativetime.php');

Now let me explain what each one does and it's structure

backend/detect_language.php

if(session_id() == '') {
     session_start();
}
header('Cache-control: private'); // IE 6 FIX


$idioma = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0, 2);

    if(isSet($idioma))
{

$lang = $idioma;


$_SESSION['lang'] = $lang;

setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}

switch ($lang) {
  case 'en':
  $lang_file = 'lang.en.php';
  break;

  case 'de':
  $lang_file = 'lang.de.php';
  break;

  case 'pt':
  $lang_file = 'lang.pt.php';
  break;

  default:
  $lang_file = 'lang.en.php';

}

include_once 'languages/'.$lang_file;

so it basically detect the browser language and get the lang file of each language, the $lang vars is working just fine on index.php but when is comes to my /backend/relativetime.php it's not showing the lang vars , here is my relative time function file:

backend/relativetime.php

function relativeTime($ts) {
        if(!ctype_digit($ts)) {
            $ts = strtotime($ts);
        }
        $diff = time() - $ts;
        if($diff == 0) {
            return $lang ['just_now'];
        } elseif($diff > 0) {
            $day_diff = floor($diff / 86400);
            if($day_diff == 0) {
                if($diff < 60) return $lang ['just_now'];
                if($diff < 120) return $lang ['one_minute_ago'];
                if($diff < 3600) return floor($diff / 60) . $lang ['s_minutes_ago'];
                if($diff < 7200) return $lang ['one_hour_ago'];
                if($diff < 86400) return floor($diff / 3600) . $lang ['hours_ago'];
            }
            if($day_diff == 1) { return $lang ['yesterday']; }
            if($day_diff < 7) { return $day_diff . $lang ['s_days_ago']; }
            if($day_diff < 31) { 
                if(ceil($day_diff / 7) == 1){
                    return ceil($day_diff / 7) . $lang ['last_week_ago'];
                } else {
                    return ceil($day_diff / 7) . $lang ['s_weeks_ago'];
                }
            }

            if($day_diff < 60) { return $lang ['last_month']; }
            return date('m-Y', $ts);
                        } else {
            $diff = abs($diff);
            $day_diff = floor($diff / 86400);
            if($day_diff == 0) {
                if($diff < 120) { return $lang ['in_a_minute']; }
                if($diff < 3600) { return $lang ['in'] . floor($diff / 60) . $lang ['minutes']; }
                if($diff < 7200) { return $lang ['in_an_hour']; }
                if($diff < 86400) { return $lang ['in'] . floor($diff / 3600) . $lang ['hours']; }
            }
            if($day_diff == 1) { return $lang ['tomorow']; }
            if($day_diff < 4) { return date('l', $ts); }
            if($day_diff < 7 + (7 - date('w'))) { return $lang ['next_week']; }
            if(ceil($day_diff / 7) < 4) { return 'in ' . ceil($day_diff / 7) . $lang ['weeks']; }
            if(date('n', $ts) == date('n') + 1) { return $lang ['next_month']; }
            return date('F Y', $ts);
        }
    }

    // Usage
    //$date  = date('Y-m-d G:i:s'); // Outputs 2017-03-19 03:17:04
    //$rel   = relativeTime($date); // Outputs 2 minutes ago, 4 hours ago, 2 days ago, 6 weeks ago, etc based on the time right now

So on index.php it's showing other ****$lang** vars** just fine, but it's not showing the $lang vars where is in the backend/relativetime.php I tried to include the backend/detect_language.php inside the backend/relativetime.php function, but also do not work,

it shows the $lang vars inside relativetime.php are undefined but it's defined inside the lang file

Only

relativetime.php $lang vars is not working

but

All others $lang vars is working just fine

Someone have a solution?

Basic example of What i need:

$userinfo['name'] = "bob";
$userinfo['lastname'] = "johnson";
function displayinfo() {
echo $userinfo['name'] . " " . $userinfo['lastname'];
}
displayinfo();

so how the array can be accessed in the displayinfo() function? this is what I need to solve this.

Otávio Barreto
  • 1,536
  • 3
  • 16
  • 35
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – aynber Jul 26 '19 at 15:09
  • Please read up on [variable scope](http://php.net/manual/en/language.variables.scope.php). Since `$lang` is not defined anywhere inside of the function, the function has no idea what it is. – aynber Jul 26 '19 at 15:10
  • 1
    Other dupe: https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and – aynber Jul 26 '19 at 15:10
  • so you think I should declare it as a `global variable`? my $lang variable is `array type`like this `$lang['language'] = 'tradution';` – Otávio Barreto Jul 26 '19 at 17:21
  • Either global or pass $lang into the function. – aynber Jul 26 '19 at 17:43
  • could you give me a example of how to to it? – Otávio Barreto Jul 26 '19 at 17:44
  • because `echo global $lang['just_now'];` is giving me a error as I told you my lang variable is a `array type` how to implement this? if I echo `$lang['just_now'];` is gives me the tradution just fine, but with global I am having a error – Otávio Barreto Jul 26 '19 at 17:53
  • I'd recommend reading those duplicate links as well as the variable scope article to see how it's done. – aynber Jul 26 '19 at 17:55
  • I read and I know how it's done, but my global is not working with a array – Otávio Barreto Jul 26 '19 at 17:56
  • I will give you a basic example so you can understand it better `$userinfo['name'] = "bob"; $userinfo['lastname'] = "johnson"; function displayinfo() { echo $userinfo['name'] . " " . $userinfo['lastname']; } displayinfo();` so how the array would be accessed in the `displayinfo()` function? – Otávio Barreto Jul 26 '19 at 18:04
  • You can do `function displayinfo($userinfo){ ...}` or `function displayinfo() { global $userinfo; ... }` – aynber Jul 26 '19 at 18:06
  • but the problem in my `$lang` vars is that it's all called `$lang['array_here'] = 'value here';` this would not work in this case because all lang vars/array are in `$lang` it's not so simple as it looks like when I try `echo global $userinfo['lastname'];` inside the function it just gives me a sintax error but it all looks ok in the code – Otávio Barreto Jul 26 '19 at 18:16

1 Answers1

0

I solved with the following:

backend/relativetime.php

function relativeTime($ts) {
//global should be here 
global $lang;
        if(!ctype_digit($ts)) {
            $ts = strtotime($ts);
        }
        $diff = time() - $ts;
        if($diff == 0) {
            return $lang ['just_now'];
        } elseif($diff > 0) {
            $day_diff = floor($diff / 86400);
            if($day_diff == 0) {
                if($diff < 60) return $lang ['just_now'];
                if($diff < 120) return $lang ['one_minute_ago'];
                if($diff < 3600) return floor($diff / 60) . $lang ['s_minutes_ago'];
                if($diff < 7200) return $lang ['one_hour_ago'];
                if($diff < 86400) return floor($diff / 3600) . $lang ['hours_ago'];
            }
            if($day_diff == 1) { return $lang ['yesterday']; }
            if($day_diff < 7) { return $day_diff . $lang ['s_days_ago']; }
            if($day_diff < 31) { 
                if(ceil($day_diff / 7) == 1){
                    return ceil($day_diff / 7) . $lang ['last_week_ago'];
                } else {
                    return ceil($day_diff / 7) . $lang ['s_weeks_ago'];
                }
            }

            if($day_diff < 60) { return $lang ['last_month']; }
            return date('m-Y', $ts);
                        } else {
            $diff = abs($diff);
            $day_diff = floor($diff / 86400);
            if($day_diff == 0) {
                if($diff < 120) { return $lang ['in_a_minute']; }
                if($diff < 3600) { return $lang ['in'] . floor($diff / 60) . $lang ['minutes']; }
                if($diff < 7200) { return $lang ['in_an_hour']; }
                if($diff < 86400) { return $lang ['in'] . floor($diff / 3600) . $lang ['hours']; }
            }
            if($day_diff == 1) { return $lang ['tomorow']; }
            if($day_diff < 4) { return date('l', $ts); }
            if($day_diff < 7 + (7 - date('w'))) { return $lang ['next_week']; }
            if(ceil($day_diff / 7) < 4) { return 'in ' . ceil($day_diff / 7) . $lang ['weeks']; }
            if(date('n', $ts) == date('n') + 1) { return $lang ['next_month']; }
            return date('F Y', $ts);
        }
    }

    // Usage
    //$date  = date('Y-m-d G:i:s'); // Outputs 2017-03-19 03:17:04
    //$rel   = relativeTime($date); // Outputs 2 minutes ago, 4 hours ago, 2 days ago, 6 weeks ago, etc based on the time right now

Instead of try access each array as global I added global only to the array $lang global $lang; and then automatically all arrays becomes global.

Otávio Barreto
  • 1,536
  • 3
  • 16
  • 35