Skip to content

Instantly share code, notes, and snippets.

@juareznjunior
Last active May 21, 2019 18:22
Show Gist options
  • Save juareznjunior/36808dd979fb656815672f2d151d95da to your computer and use it in GitHub Desktop.
Save juareznjunior/36808dd979fb656815672f2d151d95da to your computer and use it in GitHub Desktop.
Código das Divisões do Ano
<?php
/**
* Retorna codigo para
* - bimestre 12 34 56 78 910 1112
* - trimestre 123 456 789 101112
* - quadrimestre 1234 5678 9101112
* - semestre 123456 789101112
*
* @param integer $periodo
* @param string $tipo (bimestre|trimestre|quadrimestre|semestre)
* @return integer
*/
function getDivisaoReferencia($periodo, $tipo)
{
$arrayTipoRetorno = [
'bimestre' => ['^(1|2)$','^(3|4)$','^(5|6)$','^(7|8)$','^(9|10)$','^(11|12)$']
,'trimestre' => ['^(1|2|3)$','^(4|5|6)$','^(7|8|9)$','^(10|11|12)$']
,'quadrimestre' => ['^(1|2|3|4)$','^(5|6|7|8)$','^(09|10|11|12)$']
,'semestre' => ['^(1|2|3|4|5|6)$','^(7|8|9|10|11|12)$']
];
if ( isset($arrayTipoRetorno[$tipo]) ) {
// novo array para o tipo solicitado
$arrayTipoRetorno = $arrayTipoRetorno[$tipo];
// auxialar inteiro do mes
$auxMesPeriodo = (int) substr($periodo,-2);
// para cada regex da chave tipo
foreach ( $arrayTipoRetorno as $regex ) {
if ( 1 === preg_match('/'.$regex.'/', $auxMesPeriodo) ) {
return (int) preg_replace('/[^0-9]/','',$regex);
}
}
}
return 0;
}
echo getDivisaoReferencia(201804, 'bimestre')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment