I need a function in PHP that returns me if it is a national holiday or weekend, does anyone have any examples?
I need a function in PHP that returns me if it is a national holiday or weekend, does anyone have any examples?
There is no native function for this, but you can use the Holiday API, it supports holidays from Brazil and other locations, Portugal is not supported for a while.
For demonstration I did this JavaScript:
$(':button').click(function() {
var dia = $(':input[dia]').val();
var mes = $(':input[mes]').val();
var ano = $(':input[ano]').val();
var pais = $(':input[pais]').val();
$.ajax({
url: 'http://holidayapi.com/v1/holidays?country=' + pais + '&year=' + ano + '&month=' + mes + '&day=' + dia,
type: "get",
dataType: "json",
success: function(data) {
alert(data.holidays[0] != null ? data.holidays[0].name : 'Não é feriado');
}
});
});
input {
width: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><table><tr><td>Data:</td><td><inputdiavalue="21">/
<input mes value="04">/
<input ano value="2016">
</td>
</tr>
<tr>
<td>Local:</td>
<td>
<select pais>
<option value="BR">BR</option>
<option value="US">US</option>
<option value="FR">FR</option>
<option value="ES">ES</option>
</select>
</td>
</tr>
</table>
<button>VERIFICAR</button>
For PHP you can use cURL:
function verificarFeriado($data, $boolean = false) {
$param = array();
// Sua chave (exigida após 01/08/2016!), leia no final dessa postagem!
$param['key'] = '';
// Listas de países suportados!
$paisesSuportados = array('BE', 'BG', 'BR', 'CA', 'CZ', 'DE', 'ES', 'FR', 'GB', 'GT', 'HR', 'HU', 'ID', 'IN', 'IT', 'NL', 'NO', 'PL', 'PR', 'SI', 'SK', 'US');
// Define o pais para buscar feriados
$param['country'] = $paisesSuportados[2];
// Quebra a string em partes (em ano, mes e dia)
list($param['year'], $param['month'], $param['day']) = explode('-', $data);
// Converte a array em parâmetros de URL
$param = http_build_query($param);
// Conecta na API
$curl = curl_init('https://holidayapi.com/v1/holidays?'.$param);
// Permite retorno
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Obtem dados da API
$dados = json_decode(curl_exec($curl), true);
// Encerra curl
curl_close($curl);
// Retorna true/false se houver $boolean ou Nome_Do_Feriado/false se não houve $boolean
return isset($dados['holidays']['0']) ? $boolean ? true : $dados['holidays']['0']['name'] : false;
}
var_export( verificarFeriado('2016-04-21') );
// Resposta: 'Tiradentes'
var_export( verificarFeriado('2016-04-21', true) );
// Resposta: true
var_export( verificarFeriado('2016-04-20') );
// Resposta: false
var_export( verificarFeriado('2016-04-20', true) );
// Resposta: false
Important:
From 01/08/2016 you will need to get a key to the API, for this you can generate one, for free, through the site itself: link .
As of 01/08/2016 it will be necessary to use HTTPS instead of HTTP.
The function (in PHP) was updated on 07/07/2016 to support these API changes.