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.