多语言环境下PHP时间戳转相对时间的变量传递问题解决
多语言场景下的MySQL时间戳转相对时间函数变量传递问题
我正在做一个多语言场景的功能,需要写个PHP函数把MySQL时间戳转换成「几天前、一天前」这类相对时间描述。一开始写的函数是这样的:
function time_ago($timestamp, $lang) { $time_ago = strtotime($timestamp); $current_time = time(); $time_difference = $current_time - $time_ago; $seconds = $time_difference; $minutes = round($seconds / 60); $hours = round($seconds / 3600); $days = round($seconds / 86400); $weeks = round($seconds / 604800); $months = round($seconds / 2629440); $years = round($seconds / 31553280); if ($seconds <= 60){ return $lang['time_just_now']; } else if ($minutes <= 60){ if ($minutes == 1){ return $lang['time_one_minute_ago']; } else { return $lang['time_minutes_ago']; } } else if ($hours <= 24){ if ($hours == 1){ return $lang['time_one_hour_ago']; } else { return $lang['time_hours_ago']; } } else if ($days <= 7){ if ($days == 1){ return $lang['time_one_day_ago']; } else { return $lang['time_days_ago']; } } elseif($weeks <= 4.3) { if ($weeks == 1) { return $lang['time_one_week_ago']; } else { return $lang['time_weeks_ago']; } } elseif($months <= 12) { if ($months == 1) { return $lang['time_one_month_ago']; } else { return $lang['time_months_ago']; } } else { if ($years == 1) { return $lang['time_one_year_ago']; } else { return $lang['time_years_ago']; } } }
我的多语言系统是基于会话的,所有语言字符串都存在$lang数组里,比如西班牙语的配置是这样的:
$lang['time_one_day_ago'] = 'hace un día'; $lang['time_days_ago'] = "hace {$days} días";
但这里有个问题:我没法把函数里计算出来的$days变量传递给$lang数组里的字符串模板,该怎么解决呢?
解决方案
后来参考了思路,调整了函数实现,同时修改了语言文件的写法,现在可以正常传递变量了:
修改后的函数
function time_ago($timestamp) { global $lang; $time_ago = strtotime($timestamp); $current_time = time(); $time_difference = $current_time - $time_ago; $seconds = $time_difference; $minutes = round($seconds / 60); $hours = round($seconds / 3600); $days = round($seconds / 86400); $weeks = round($seconds / 604800); $months = round($seconds / 2629440); $years = round($seconds / 31553280); if ($seconds <= 60){ return $lang['time_just_now']; } else if ($minutes <= 60) { if ($minutes == 1){ return $lang['time_one_minute_ago']; } else { return sprintf(_($lang['time_minutes_ago']), $minutes); } } else if ($hours <= 24){ if ($hours == 1){ return $lang['time_one_hour_ago']; } else { return sprintf(_($lang['time_hours_ago']), $hours); } } else if ($days <= 7){ if ($days == 1){ return $lang['time_one_day_ago']; } else { return sprintf(_($lang['time_days_ago']), $days); } } elseif($weeks <= 4.3) { if ($weeks == 1) { return $lang['time_one_week_ago']; } else { return sprintf(_($lang['time_weeks_ago']), $weeks); } } elseif($months <= 12) { if ($months == 1) { return $lang['time_one_month_ago']; } else { return sprintf(_($lang['time_months_ago']), $months); } } else { if ($years == 1) { return $lang['time_one_year_ago']; } else { return sprintf(_($lang['time_years_ago']), $years); } } }
对应语言文件的写法
把原来的变量嵌入改成占位符%s,这样就能通过sprintf传递函数里计算出的数值了:
$lang['time_one_year_ago'] = 'hace un año'; $lang['time_years_ago'] = 'hace %s años';
感谢arkadianriver提供的思路!
内容的提问来源于stack exchange,提问作者Derian Andrés
相关产品推荐
相关产品推荐

