1
I need a PHP function that returns me if it is a national holiday or weekend, someone has some example?
1
I need a PHP function that returns me if it is a national holiday or weekend, someone has some example?
5
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 currently supported.
For demo I made 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>
<input dia value="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:
As of 01/08/2016 it will be necessary to get a key to the API, for this generate one, for free, through the site itself: https://holidayapi.com/.
From 01/08/2016 it will be necessary to use HTTPS instead of HTTP.
The function (in PHP) was updated on 07/12/2016 to support these API changes.
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
There is no such function. Checking holiday is complicated. You have to have a database with all of them, even so you can change.
– Maniero
National holiday is impossible to represent natively.
– rray
How to create a list of dates of the year in php, skipping weekends?
– rray
Working day script
– rray