ricardocoimbra Posted October 23, 2007 at 08:46 AM Report Share #142334 Posted October 23, 2007 at 08:46 AM Campo data: 2007 - 10 - 22 Imaginemos que quero dividir esta data em 3 meses assim? 2007 - 10 - 22 2007 - 11 - 22 2007 - 12 - 22 Como poderia fazer isto em php? Link to comment Share on other sites More sharing options...
djthyrax Posted October 23, 2007 at 05:45 PM Report Share #142464 Posted October 23, 2007 at 05:45 PM $data = explode(" - ", $data); $mes = array($data[1]); $mes[] = ++$data[1]; $mes[] = ++$data[1]; foreach($mes as $d) echo $data[0]." - ".$d." - ".$data[2]; Não peças ajuda por PM! A tua dúvida vai ter menos atenção do que se for postada na secção correcta do fórum! Link to comment Share on other sites More sharing options...
naoliveira Posted October 29, 2007 at 02:37 PM Report Share #143759 Posted October 29, 2007 at 02:37 PM $data = explode(" - ", $data); $mes = array($data[1]); $mes[] = ++$data[1]; $mes[] = ++$data[1]; foreach($mes as $d) echo $data[0]." - ".$d." - ".$data[2]; djthyrax, não é tão simples assim, supõe que o mês inicial é 12, isto é, 2007-12-22, se fizeres como indicas vais ter o mês 14 e 15?!?! Para remediar podes fazer assim if ($mes >12) { $mes=$mes-12 $ano=$ano+1 } a solução está nesta função (este texto foi sacado do manual do php): mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input. For example, each of the following lines produces the string "Jan-01-1998". <?php echo date("M-d-Y", mktime(0, 0, 0, 12, 32, 1997)); echo date("M-d-Y", mktime(0, 0, 0, 13, 1, 1997)); echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 1998)); echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 98)); ?> Link to comment Share on other sites More sharing options...
djthyrax Posted October 29, 2007 at 06:09 PM Report Share #143785 Posted October 29, 2007 at 06:09 PM Nem me lembrei desse promenor. Anyway: function incr($array, $what){ switch strtolower($what){ case "y": $array[0]++; break; case "m": if($array[1] > 11){ $array[0]++; $array[1] = 0; } $array[1]++; break; case "d": $dias = array( 1 => 31, 2 => (date("L", mktime(0, 0, 0, 2, 0, $array[0])) ? 29 : 28), 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31); if($array[2] == $dias[$array[1]]){ $array = incr($array, "m"); $array[2] = 0; } $array[2]++; break; } return $array; } $data = explode(" - ", $data); $novasDatas = array(); $novasDatas[] = incr($data, "m"); $novasDatas[] = incr($novasDatas[0], "m"); Não testei. PS: O (date("L", mktime(0, 0, 0, 2, 0, $array[0])) ? 29 : 28) vai pôr uma limitação na função. Não peças ajuda por PM! A tua dúvida vai ter menos atenção do que se for postada na secção correcta do fórum! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now