Incomplete week name in strftime return

0

I'm trying to print the day of the week, according to a particular timestamp, with the function strftime .

I'm doing the following:

setlocale(LC_ALL, 'pt_BR', 'pt_BR.iso-8859-1', 'pt_BR.utf-8', 'portuguese');

echo strftime('%A', strtotime('now'));

I was expecting the result quinta-feira , however I'm returned quinta .

Why did this happen? The result should not be quinta-feira ?

I'm using Ubuntu , with PHP 5.5.9-1 ubuntu 4.11

I am using Laravel 4 and this problem occurs in object Carbon\Carbon .

Example:

#filters.php
App::before(function ()
{
     setlocale(LC_ALL, 'pt_BR', 'pt_BR.iso-8859-1', 'pt_BR.utf-8', 'portuguese');
});

#view qualquer
$usuario->created_at->formatLocalized('%A'); // quinta

Comment : I do not want to use array with the list of all days of the week (thank you)

    
asked by anonymous 16.07.2015 / 15:22

2 answers

2

PHP removes locale information from the operating system where it is running. As described in the documentation:

  

Note: The setlocale () return value depends on the system on which the   PHP is running. It returns exactly what the function   setlocale system returns.

So if the operating system displays the days of the week without the "Thursday" the dates displayed by the locale will be like this.

Using your own example:

<?php
//teste.php

setlocale(LC_ALL, 'pt_BR', 'pt_BR.iso-8859-1', 'pt_BR.utf-8', 'portuguese');
echo strftime('%A', strtotime('now'));
echo PHP_EOL;

When I run this script:

adirkuhn: ~ $ php teste.php 
quinta

Okay, so comparing with my operating system. First in the current locale (English):

adirkuhn: ~ $ date +%A
Thursday

And now with the locale in pt_BR:

adirkuhn: ~ $ LC_TIME=pt_BR.UTF8 date +%A
quinta

So you have two options or add the "Monday" in the hand with PHP even though it's easier.

Or change the operating system locale file. My test with the locale changed:

...
LC_TIME
abday   "<U0044><U006F><U006D>";"<U0053><U0065><U0067>";/
        "<U0054><U0065><U0072>";"<U0051><U0075><U0061>";/
        "<U0051><U0075><U0069>";"<U0053><U0065><U0078>";/
        "<U0053><U00E1><U0062>"
day     "<U0064><U006F><U006D><U0069><U006E><U0067><U006F>";/
        "<U0073><U0065><U0067><U0075><U006E><U0064><U0061><U002D><U0066><U0065><U0069><U0072><U0061>";/
        "<U0074><U0065><U0072><U00E7><U0061><U002D><U0066><U0065><U0069><U0072><U0061>";/
        "<U0071><U0075><U0061><U0072><U0074><U0061><U002D><U0066><U0065><U0069><U0072><U0061>";/
        "<U0071><U0075><U0069><U006E><U0074><U0061><U002D><U0066><U0065><U0069><U0072><U0061>";/
        "<U0073><U0065><U0078><U0074><U0061><U002D><U0066><U0065><U0069><U0072><U0061>";/
        "<U0073><U00E1><U0062><U0061><U0064><U006F>"
...

Test the date by system:

adirkuhn: ~ $ LC_TIME=pt_BR date +%A                                                                                  
quinta-feira

And now the test with PHP:

adirkuhn: ~ $ php teste.php                                                                                           
quinta-feira
    
16.07.2015 / 17:05
0

With dates always several possible solutions. Here are a few:

$diasSemana = array('domingo', 'segunda-feira', 'terça-feira', ...);
echo $diasSemana[strftime('%w', strtotime('now')];
$now = strtotime('now');
echo strftime('%A', $now);

$decimalDiaSemana = strftime('%w', $now);
if ($decimalDiaSemana > 0 && $decimalDiaSemana < 6) {
    echo '-feira';
}
$now = strtotime('now');
$sufixo = array('', '-feira', '-feira', '-feira', '-feira', '-feira', '');
echo strftime('%A', $now) . $sufixo[strftime('%w', $now);
    
16.07.2015 / 15:57