Friday, 7 October 2016

php - Problem with helper function and sending variable with redirect in Laravel



I have a problem with my helper function called swe_date.
It outputs nothing.
If I don`t use it, all is good.



I have done the composer dump-autoload thing and put
"app/helpers.php" in the composer.json file.



My helper function looks like this.




 if (! function_exists('swe_date'))
{
function swe_date($date)
{
setlocale(LC_TIME, 'sv_SV');
return strftime('%A %d %B %Y %H:%M',strtotime($date));
}
}



My controller where I try to send my variable with my helper function.
Notice when I do a dd(swe_date($suspended->suspended_until)) I got:
b"söndag 22 september 2019 00:00"
Don't know where the "b" comes from.



  $date = swe_date($suspended->suspended_until);
//dd(swe_date($suspended->suspended_until));
return redirect('/login')->with('date',$date);



The view where I try to show the message.



 @if(Session('date'))

{{Session('date')}}

@endif



composer.json



    "autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],

"files": [
"app/helpers.php"
]
},

Answer



From php.net:




The "locale" always depend on the server configuration




The locale string need to be supported by the server.



Sometimes there are diferents charsets for a language, like
"pt_BR.utf-8"




so if your server is windows try:



setlocale(LC_ALL, 'sv-SE.utf-8');



on linux try:



setlocale(LC_ALL, 'sv_SE.utf-8');


I think the UTF-8 will solve your "b" issue.



Also on linux make sure your language is supported "sudo locale -a"




if not follow this to install


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...