Jump to content

Dividir data - Duvida


ricardocoimbra

Recommended Posts

$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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.