Date function in Portuguese
I have this code
<table class="table table-hover table-bordered table-condensed lista-clientes table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>
<center>IP Address</center>
</th>
<th>
<center>Banned By</center>
</th>
<th>
<center>Reason</center>
</th>
<th>
<center>Banned On</center>
</th>
<th>
<center>Banned Until</center>
</th>
</tr>
</thead>
<tbody>
<?php
date_default_timezone_set('UTC');
while($row = $retval->fetch_assoc()) {
if($row['banner'] == null) {
$row['banner'] = 'Console';
}
// <<-----------------Ban Date Converter------------>> //
$timeEpoch = $row['time'];
$timeConvert = $timeEpoch / 1000;
$timeResult = date('F j, Y, g:i a', $timeConvert);
// <<-----------------Expiration Time Converter------------>> //
$expiresEpoch = $row['expires'];
$expiresConvert = $expiresEpoch / 1000;
$expiresResult = date('F j, Y, g:i a', $expiresConvert);
?>
<tr>
<td>
<?php
$ip = $row['ip'];
$array = explode(".", $ip);
$numbers = $array[0] . "." . $array[1] . "." . $array[2];
$numbers .= ".";
for($i = 0; $i < strlen($array[3]); $i++) {
$numbers .= "*";
}
echo $numbers;
?>
</td>
<td><?php echo $row['banner'];?></td>
<td style="width: 30%;"><?php echo $row['reason'];?></td>
<td><?php echo $timeResult;?></td>
<td><?php if($row['expires'] == 0) {
echo 'ETERNO';
} else {
echo $expiresResult; }?></td>
</tr>
<?php }
$conn->close();
echo "</tbody></table>";
?>
But I want to leave the date, times etc... In Portuguese, however, I am not getting it... So this is getting in English. Could anyone help me ?
2
1 answers
To use dates with location in "pt-BR "(Brazil) or " pt-PT "(Portugal), two functions are required: setlocale e strftime.
The difference between date
and strftime
(in addition to the parameters) is that strftime
works with the given location, while date
ignores these values.
In the function setlocale
, we can decide if we want to apply the location to all fields or just date, money, time etc. To apply with dates only, use setlocale(LC_TIME, "<Idioma>");
<?php
/* Localização */
setlocale(LC_ALL, "pt-BR");
/* Ignora a localização */
echo date("F j, Y, g:i a").PHP_EOL;
/* Aplica a data com localização */
echo strftime("%B %d, %G, %I:%M %p");
Your code should look like this:
<?php
date_default_timezone_set('UTC');
setlocale(LC_ALL, "pt-BR");
while($row = $retval->fetch_assoc()) {
if($row['banner'] == null) {
$row['banner'] = 'Console';
}
// <<-----------------Ban Date Converter------------>> //
$timeEpoch = $row['time'];
$timeConvert = $timeEpoch / 1000;
$timeResult = strftime("%B %d, %G, %I:%M %p", $timeConvert);
// <<-----------------Expiration Time Converter------------>> //
$expiresEpoch = $row['expires'];
$expiresConvert = $expiresEpoch / 1000;
$expiresResult = strftime("%B %d, %G, %I:%M %p", $expiresConvert);
?>
To know other parameters of the function
stftime
, simply access the Documentation
1
Author: Valdeir Psr, 2018-02-20 19:20:16