Comparando la fecha futura (cercana) con la fecha actual, cómo

check whether the the future date is in the same month as current one? I tried...

$Current_Date=time(); // today - 31st July
$Future_Date =mkdate(~1st aug~); // tomorrow - 1st Aug
if(("m",$Future_Date )>date("m",$Current_Date ))
    echo date("j",$Current_Date ) . "SAME MONTH!!";
else
    echo date("j",$Current_Date ) . date("m",$Current_Date );

But say if $Future_Date~1st July Next year, it still shows "SAME MONTH".

I'm making kind of a countdown thing which counts how many months left till the $Future_Date. I wanted to display just the date if it's in the same month, but date with its month if it's in the next or any later month.

preguntado el 31 de julio de 12 a las 14:07

1 Respuestas

You can use the dateinterval class in PHP for this: http://php.net/manual/en/class.dateinterval.php or you can also compare on year as well:

if(("m",$Future_Date )==date("m",$Current_Date ) && ("Y",$Future_Date )==date("Y",$Current_Date ))
    echo date("j",$Current_Date ) . "SAME MONTH!!";
else
    echo date("j",$Current_Date ) . date("m",$Current_Date );

Respondido 31 Jul 12, 14:07

dateinterval is perfect. I hadn't known of it. I have quite a few thhings depending on mkdate()'s input format. Is there a way to add hours too into this declaration: $Future = new DateTime('2010-01-01'); edit:just hours. - reflejo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.