Incomplete week name on strftime return

Asked

Viewed 474 times

0

I am trying to print the day of the week, according to a certain timestamp, with the function strftime.

I’m doing it this way:

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

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

I expected the result quinta-feira, but I am returned quinta.

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

I am using the Ubuntu, with the PHP 5.5.9-1 ubuntu 4.11

I’m using Laravel 4 and this problem occurs in the 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

Observing: I don’t want to wear one array with the list of all days of the week (thank you)

  • Before publishing as an answer see if this suits you: http://www.tiagomatos.com/blog/dia-da-semana-mes-e-ano-em-portugues-utilizando-php I know you don’t want to use array but the solution seems very simple

  • I don’t want to do so. I’m using Laravel 4. And I have to return this through the Object Carbon\Carbon, through the formatLocalized function. I cannot create "alterative methods".

  • I understand I’m learning Cakephp and he’s also well xaropi

  • I have a slight impression that this is configuration of Ubuntu. My date returns Quinta as in the operating system

  • not how to concatenate or edit the value of this object with the value of the days of the week array?

  • I may be wrong, but I believe no language returns the date with the suffix feira.

Show 1 more comment

2 answers

2


PHP removes locale information from the operating system it is running on. 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 of the system returns.

So if the operating system displays the days of the week without the "-fair" 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 compare it to my operating system. First in the current locale (English):

adirkuhn: ~ $ date +%A
Thursday

And now with the pt_BR locale:

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

So you have two options or add the "-fair" in hand with PHP even though it is easier.

Or changes the operating system locale file. My test with the modified locale:

...
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>"
...

Date testing by the system:

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

And now the PHP test:

adirkuhn: ~ $ php teste.php                                                                                           
quinta-feira
  • I will mark as solved, because, since it is the operating system, at least I can talk to the technical support staff of the hosting for them to change (or put in the same hand :)

0

With dates to always several possible solutions. Here are some:

$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);
  • 1

    If it is to do with array, I prefer to do so echo preg_replace('/^(segunda|terça|quarta|quinta|sexta)$/i', '$1-feira', strftime('%A', strtotime('now')));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.