Compare and replace index array

0

The problem is the following, I'm creating daily OS graphs, only coming from the day bank in English, I would like to replace each index with the Portuguese value My code

    public function osSemanal($id_company)
{        
    $periodo = date("Y-m-d H:i:s", strtotime('-7 days'));
    $sql = $this->db->prepare("select DAYNAME(dt_chegada) as dia, count(id) as os_gerada from os where id_company= :id_company
    and dt_chegada >= :periodo group by DAYNAME(dt_chegada) order by dt_chegada");
    $sql->bindValue(":id_company", $id_company);
    $sql->bindValue(":periodo", $periodo);
    $sql->execute();
    $result = $sql->fetchAll(PDO::FETCH_OBJ);

    foreach ($result as $res):            
        $dados[$res->dia] = $res->os_gerada;
      endforeach;     

    echo json_encode($dados);
}

Comes from the bank:

[0]
"dia"=> "Friday",
"os_gerada"=> 1

[1]
"dia"=> "Sunday",
"os_gerada"=> 4

It is generated:

"Friday" => 1,
"Sunday" => 4

Expected result:

"Sexta" => 1,
"Domingo"=> 4
    
asked by anonymous 11.12.2017 / 18:03

3 answers

3

You can use strftime after setting the location:

setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');

echo ucfirst(strftime('%A', strtotime('Monday'))); // Segunda
echo ucfirst(strftime('%A', strtotime('Sunday'))); // Domingo
echo ucfirst(strftime('%A', strtotime('Wednesday'))); // Quarta
echo ucfirst(strftime('%A', strtotime('Friday'))); // Sexta
    
11.12.2017 / 18:22
3

You can create an array with the translation of the days of the week, then go through your array translating the key this way:

<?php

$arr1 = ['Monday' => 1, 'Tuesday' => 2, 'Wednesday' => 4];
$arrTranslate = ['Monday' => 'Segunda', 'Tuesday' => 'Terça', 'Wednesday' => 'Quarta'];

$newArray = [];
foreach($arr1 as $key => $value){

    $newArray[$arrTranslate[$key]] = $value;

}

print_r($newArray);

Or you can also run your query before calling another command in MySQL that changes the language:

SET lc_time_names = 'pt_BR';

So your method would look like this:

public function osSemanal($id_company)
{
    $periodo = date("Y-m-d H:i:s", strtotime('-7 days'));
    $sqlAux = $this->db->prepare("SET lc_time_names = 'pt_BR'");
    $sqlAux->execute();
    $sql = $this->db->prepare("select DAYNAME(dt_chegada) as dia, count(id) as os_gerada from os where id_company= :id_company
    and dt_chegada >= :periodo group by DAYNAME(dt_chegada) order by dt_chegada");
    $sql->bindValue(":id_company", $id_company);
    $sql->bindValue(":periodo", $periodo);
    $sql->execute();
    $result = $sql->fetchAll(PDO::FETCH_OBJ);

    foreach ($result as $res):
        $dados[$res->dia] = $res->os_gerada;
    endforeach;

    echo json_encode($dados);
}
    
11.12.2017 / 18:24
0

You can translate the days of the week with Switch or with the example IF below:

<?php
$DIA_en = array();
// Armazenar os dias da semana em uma array
$DIA_en['DIA'] = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
for($x = 0; $x <= count($DIA_en['DIA']); $x++) {
    if( $DIA_en['DIA'][$x] == "Monday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Segunda";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Tuesday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Terça";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Wednesday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Quarta";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Thursday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Quinta";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Friday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Sexta";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Saturday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Sábado";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    } else if ( $DIA_en['DIA'][$x] == "Sunday" ) {
        echo "Antes: " . $DIA_en['DIA'][$x] . "<br>";
        $DIA_en['DIA'][$x] = "Domingo";
        echo "Depois: " . $DIA_en['DIA'][$x] . "<hr>";
    }
}
?>

Will return:

Before: Monday

Then: Second

Before: Tuesday

After: Tuesday

Before: Wednesday

After: Fourth

Before: Thursday

After: Thursday

Before: Friday

After: Sixth

Before: Saturday

After: Saturday

Before: Sunday

After: Sunday

    
11.12.2017 / 19:13