¿Como comparar un dato entre dos valores?

¿Cómo puedo comparar un valor entre dos valores?

Por ejemplo:

Quiero hacer que PHP asigne un signo de horóscopo de cada usuario cogiendo su fecha de nacimiento:

$fn = "20/10"

Quiero que si $fn es mayor o igual que "22/09" y menor o igual que "21/10" haga un nuevo valor &horo = "Libra", y así con todos los signos.

He hecho este ejemplo, pero no me funciona correctamente:

¿
<?
Function horoscop($date){
    $newdate = DateTime::createFromFormat('Y-m-d', $date);
    $newdate = $newdate->format('d/m');
    switch($newdate){
        case ($newdate>="22/03" && $newdate<="21/04");
        $horo = "<b>&#9800;</b> Aries";
        break;
        case ($newdate>="22/04" && $newdate<="21/05");
        $horo = "<b>&#9801;</b> Tauro";
        break;
        case ($newdate>="22/05" && $newdate<="21/06");
        $horo = "<b>&#9802;</b> Geminis";
        break;
        case ($newdate>="22/06" && $newdate<="21/07");
        $horo = "<b>&#9803;</b> Cancer";
        break;
        case ($newdate>="22/07" && $newdate<="21/08");
        $horo = "<b>&#9804;</b> Leo";
        break;
        case ($newdate>="22/08" && $newdate<="21/09");
        $horo = "<b>&#9805;</b> Virgo";
        break;
        case ($newdate>="22/09" && $newdate<="21/10");
        $horo = "<b>&#9806;</b> Libra";
        break;
        case ($newdate>="22/10" && $newdate<="21/11");
        $horo = "<b>&#9807;</b> Escorpio";
        break;
        case ($newdate>="22/11" && $newdate<="21/12");
        $horo = "<b>&#9808;</b> Sagitario";
        break;
        case ($newdate>="22/12" && $newdate<="21/01");
        $horo = "<b>&#9809;</b> Capricornio";
        break;
        case ($newdate>="22/01" && $newdate<="21/02");
        $horo = "<b>&#9810;</b> Acuario";
        break;
        case ($newdate>="22/02" && $newdate<="21/03");
        $horo = "<b>&#9811;</b> Piscis";
        break;
        return $horo;
    }
    return $horo;
}
?>

Antes de usar el switch también lo hice con el if, pero nada.

¿Alguna solución?

1 Respuesta

Respuesta
1

¿Realmente en cual de los "case" esta entrando? Crea un "default" para que por lo menos entre en ese y puedar comprobar si entra en alguno.

De todas formas, lo suyo seria crear la variable $horo junto con $newdate para luego poder modificarla ya que teóricamente una variable solo tiene vida dentro de las "{}" que se crea. Ese puede ser tu error también.

Miralo y me cuentas

Gracias por tu respuesta.

La cosa es que un default devolvería nulo. El dato $newdate es una fecha formateada y en los case es un string, por lo que la operación <= o >= no puede ver mas allá de 22/10 o mas atrás ya que no es una fecha sino un string, e probado con usar funciones como strtotime, o generando nuevas fechas por cada case para que pueda hacer la comparativa pero en ninguno de los casos a funcionado correctamente, así que pensé en mi ultima solución, que fue hacerlo de forma bruta :D... Así que cogí el formato de fecha y fui día por día asignando el signo con if. Se que no es la forma mas adecuada de programar pero no se me ocurría ninguna solución mas, y tampoco encontraba nada en ningún foro.

Te adjunto mi version para que la veas si quieres.

function horoscop($date){
    $dia = substr($date,8,2);
    $mes = substr($date,5,2);
    $horo = $dia."/".$mes;
    $h0 = "<b><a title=\"Aries\">&#9800;</a></b>";
    $h1 = "<b><a title=\"Tauro\">&#9801;</a></b>";
    $h2 = "<b><a title=\"Geminis\">&#9802;</a></b>";
    $h3 = "<b><a title=\"Cancer\">&#9803;</a></b>";
    $h4 = "<b><a title=\"Leo\">&#9804;</a></b>";
    $h5 = "<b><a title=\"Virgo\">&#9805;</a></b>";
    $h6 = "<b><a title=\"Libra\">&#9806;</a></b>";
    $h7 = "<b><a title=\"Escorpio\">&#9807;</a></b>";
    $h8 = "<b><a title=\"Sagitario\">&#9808;</a></b>";
    $h9 = "<b><a title=\"Capricornio\">&#9809;</a></b>";
    $h10 = "<b><a title=\"Acuario\">&#9810;</a></b>";
    $h11 = "<b><a title=\"Piscis\">&#9811;</a></b>";
 // Aries
    if($horo=="22/03"){$h = $h0;}
    if($horo=="23/03"){$h = $h0;}
    if($horo=="24/03"){$h = $h0;}
    if($horo=="25/03"){$h = $h0;}
    if($horo=="26/03"){$h = $h0;}
    if($horo=="27/03"){$h = $h0;}
    if($horo=="28/03"){$h = $h0;}
    if($horo=="29/03"){$h = $h0;}
    if($horo=="30/03"){$h = $h0;}
    if($horo=="31/03"){$h = $h0;}
    if($horo=="01/04"){$h = $h0;}
    if($horo=="02/04"){$h = $h0;}
    if($horo=="03/04"){$h = $h0;}
    if($horo=="04/04"){$h = $h0;}
    if($horo=="05/04"){$h = $h0;}
    if($horo=="06/04"){$h = $h0;}
    if($horo=="07/04"){$h = $h0;}
    if($horo=="08/04"){$h = $h0;}
    if($horo=="09/04"){$h = $h0;}
    if($horo=="10/04"){$h = $h0;}
    if($horo=="11/04"){$h = $h0;}
    if($horo=="12/04"){$h = $h0;}
    if($horo=="13/04"){$h = $h0;}
    if($horo=="14/04"){$h = $h0;}
    if($horo=="15/04"){$h = $h0;}
    if($horo=="16/04"){$h = $h0;}
    if($horo=="17/04"){$h = $h0;}
    if($horo=="18/04"){$h = $h0;}
    if($horo=="19/04"){$h = $h0;}
    if($horo=="20/04"){$h = $h0;}
    if($horo=="21/04"){$h = $h0;}
 // Tauro
    if($horo=="22/04"){$h = $h1;}
    if($horo=="23/04"){$h = $h1;}
    if($horo=="24/04"){$h = $h1;}
    if($horo=="25/04"){$h = $h1;}
    if($horo=="26/04"){$h = $h1;}
    if($horo=="27/04"){$h = $h1;}
    if($horo=="28/04"){$h = $h1;}
    if($horo=="29/04"){$h = $h1;}
    if($horo=="30/04"){$h = $h1;}
    if($horo=="31/04"){$h = $h1;}
    if($horo=="01/05"){$h = $h1;}
    if($horo=="02/05"){$h = $h1;}
    if($horo=="03/05"){$h = $h1;}
    if($horo=="04/05"){$h = $h1;}
    if($horo=="05/05"){$h = $h1;}
    if($horo=="06/05"){$h = $h1;}
    if($horo=="07/05"){$h = $h1;}
    if($horo=="08/05"){$h = $h1;}
    if($horo=="09/05"){$h = $h1;}
    if($horo=="10/05"){$h = $h1;}
    if($horo=="11/05"){$h = $h1;}
    if($horo=="12/05"){$h = $h1;}
    if($horo=="13/05"){$h = $h1;}
    if($horo=="14/05"){$h = $h1;}
    if($horo=="15/05"){$h = $h1;}
    if($horo=="16/05"){$h = $h1;}
    if($horo=="17/05"){$h = $h1;}
    if($horo=="18/05"){$h = $h1;}
    if($horo=="19/05"){$h = $h1;}
    if($horo=="20/05"){$h = $h1;}
    if($horo=="21/05"){$h = $h1;}
 // Geminis
 if($horo=="22/05"){$h = $h2;}
 if($horo=="23/05"){$h = $h2;}
 if($horo=="24/05"){$h = $h2;}
 if($horo=="25/05"){$h = $h2;}
 if($horo=="26/05"){$h = $h2;}
 if($horo=="27/05"){$h = $h2;}
 if($horo=="28/05"){$h = $h2;}
 if($horo=="29/05"){$h = $h2;}
 if($horo=="30/05"){$h = $h2;}
 if($horo=="31/05"){$h = $h2;}
 if($horo=="01/06"){$h = $h2;}
 if($horo=="02/06"){$h = $h2;}
 if($horo=="03/06"){$h = $h2;}
 if($horo=="04/06"){$h = $h2;}
 if($horo=="05/06"){$h = $h2;}
 if($horo=="06/06"){$h = $h2;}
 if($horo=="07/06"){$h = $h2;}
 if($horo=="08/06"){$h = $h2;}
 if($horo=="09/06"){$h = $h2;}
 if($horo=="10/06"){$h = $h2;}
 if($horo=="11/06"){$h = $h2;}
 if($horo=="12/06"){$h = $h2;}
 if($horo=="13/06"){$h = $h2;}
 if($horo=="14/06"){$h = $h2;}
 if($horo=="15/06"){$h = $h2;}
 if($horo=="16/06"){$h = $h2;}
 if($horo=="17/06"){$h = $h2;}
 if($horo=="18/06"){$h = $h2;}
 if($horo=="19/06"){$h = $h2;}
 if($horo=="20/06"){$h = $h2;}
 if($horo=="21/06"){$h = $h2;}
 // Cancer
 if($horo=="22/06"){$h = $h3;}
 if($horo=="23/06"){$h = $h3;}
 if($horo=="24/06"){$h = $h3;}
 if($horo=="25/06"){$h = $h3;}
 if($horo=="26/06"){$h = $h3;}
 if($horo=="27/06"){$h = $h3;}
 if($horo=="28/06"){$h = $h3;}
 if($horo=="29/06"){$h = $h3;}
 if($horo=="30/06"){$h = $h3;}
 if($horo=="31/06"){$h = $h3;}
 if($horo=="01/07"){$h = $h3;}
 if($horo=="02/07"){$h = $h3;}
 if($horo=="03/07"){$h = $h3;}
 if($horo=="04/07"){$h = $h3;}
 if($horo=="05/07"){$h = $h3;}
 if($horo=="06/07"){$h = $h3;}
 if($horo=="07/07"){$h = $h3;}
 if($horo=="08/07"){$h = $h3;}
 if($horo=="09/07"){$h = $h3;}
 if($horo=="10/07"){$h = $h3;}
 if($horo=="11/07"){$h = $h3;}
 if($horo=="12/07"){$h = $h3;}
 if($horo=="13/07"){$h = $h3;}
 if($horo=="14/07"){$h = $h3;}
 if($horo=="15/07"){$h = $h3;}
 if($horo=="16/07"){$h = $h3;}
 if($horo=="17/07"){$h = $h3;}
 if($horo=="18/07"){$h = $h3;}
 if($horo=="19/07"){$h = $h3;}
 if($horo=="20/07"){$h = $h3;}
 if($horo=="21/07"){$h = $h3;}
 // Leo
 if($horo=="22/07"){$h = $h4;}
 if($horo=="23/07"){$h = $h4;}
 if($horo=="24/07"){$h = $h4;}
 if($horo=="25/07"){$h = $h4;}
 if($horo=="26/07"){$h = $h4;}
 if($horo=="27/07"){$h = $h4;}
 if($horo=="28/07"){$h = $h4;}
 if($horo=="29/07"){$h = $h4;}
 if($horo=="30/07"){$h = $h4;}
 if($horo=="31/07"){$h = $h4;}
 if($horo=="01/08"){$h = $h4;}
 if($horo=="02/08"){$h = $h4;}
 if($horo=="03/08"){$h = $h4;}
 if($horo=="04/08"){$h = $h4;}
 if($horo=="05/08"){$h = $h4;}
 if($horo=="06/08"){$h = $h4;}
 if($horo=="07/08"){$h = $h4;}
 if($horo=="08/08"){$h = $h4;}
 if($horo=="09/08"){$h = $h4;}
 if($horo=="10/08"){$h = $h4;}
 if($horo=="11/08"){$h = $h4;}
 if($horo=="12/08"){$h = $h4;}
 if($horo=="13/08"){$h = $h4;}
 if($horo=="14/08"){$h = $h4;}
 if($horo=="15/08"){$h = $h4;}
 if($horo=="16/08"){$h = $h4;}
 if($horo=="17/08"){$h = $h4;}
 if($horo=="18/08"){$h = $h4;}
 if($horo=="19/08"){$h = $h4;}
 if($horo=="20/08"){$h = $h4;}
 if($horo=="21/08"){$h = $h4;}
 // Virgo
 if($horo=="22/08"){$h = $h5;}
 if($horo=="23/08"){$h = $h5;}
 if($horo=="24/08"){$h = $h5;}
 if($horo=="25/08"){$h = $h5;}
 if($horo=="26/08"){$h = $h5;}
 if($horo=="27/08"){$h = $h5;}
 if($horo=="28/08"){$h = $h5;}
 if($horo=="29/08"){$h = $h5;}
 if($horo=="30/08"){$h = $h5;}
 if($horo=="31/08"){$h = $h5;}
 if($horo=="01/09"){$h = $h5;}
 if($horo=="02/09"){$h = $h5;}
 if($horo=="03/09"){$h = $h5;}
 if($horo=="04/09"){$h = $h5;}
 if($horo=="05/09"){$h = $h5;}
 if($horo=="06/09"){$h = $h5;}
 if($horo=="07/09"){$h = $h5;}
 if($horo=="08/09"){$h = $h5;}
 if($horo=="09/09"){$h = $h5;}
 if($horo=="10/09"){$h = $h5;}
 if($horo=="11/09"){$h = $h5;}
 if($horo=="12/09"){$h = $h5;}
 if($horo=="13/09"){$h = $h5;}
 if($horo=="14/09"){$h = $h5;}
 if($horo=="15/09"){$h = $h5;}
 if($horo=="16/09"){$h = $h5;}
 if($horo=="17/09"){$h = $h5;}
 if($horo=="18/09"){$h = $h5;}
 if($horo=="19/09"){$h = $h5;}
 if($horo=="20/09"){$h = $h5;}
 if($horo=="21/09"){$h = $h5;}
 // Libra
 if($horo=="22/09"){$h = $h6;}
 if($horo=="23/09"){$h = $h6;}
 if($horo=="24/09"){$h = $h6;}
 if($horo=="25/09"){$h = $h6;}
 if($horo=="26/09"){$h = $h6;}
 if($horo=="27/09"){$h = $h6;}
 if($horo=="28/09"){$h = $h6;}
 if($horo=="29/09"){$h = $h6;}
 if($horo=="30/09"){$h = $h6;}
 if($horo=="31/09"){$h = $h6;}
 if($horo=="01/10"){$h = $h6;}
 if($horo=="02/10"){$h = $h6;}
 if($horo=="03/10"){$h = $h6;}
 if($horo=="04/10"){$h = $h6;}
 if($horo=="05/10"){$h = $h6;}
 if($horo=="06/10"){$h = $h6;}
 if($horo=="07/10"){$h = $h6;}
 if($horo=="08/10"){$h = $h6;}
 if($horo=="09/10"){$h = $h6;}
 if($horo=="10/10"){$h = $h6;}
 if($horo=="11/10"){$h = $h6;}
 if($horo=="12/10"){$h = $h6;}
 if($horo=="13/10"){$h = $h6;}
 if($horo=="14/10"){$h = $h6;}
 if($horo=="15/10"){$h = $h6;}
 if($horo=="16/10"){$h = $h6;}
 if($horo=="17/10"){$h = $h6;}
 if($horo=="18/10"){$h = $h6;}
 if($horo=="19/10"){$h = $h6;}
 if($horo=="20/10"){$h = $h6;}
 if($horo=="21/10"){$h = $h6;}
 // Escorpio
 if($horo=="22/10"){$h = $h7;}
 if($horo=="23/10"){$h = $h7;}
 if($horo=="24/10"){$h = $h7;}
 if($horo=="25/10"){$h = $h7;}
 if($horo=="26/10"){$h = $h7;}
 if($horo=="27/10"){$h = $h7;}
 if($horo=="28/10"){$h = $h7;}
 if($horo=="29/10"){$h = $h7;}
 if($horo=="30/10"){$h = $h7;}
 if($horo=="31/10"){$h = $h7;}
 if($horo=="01/11"){$h = $h7;}
 if($horo=="02/11"){$h = $h7;}
 if($horo=="03/11"){$h = $h7;}
 if($horo=="04/11"){$h = $h7;}
 if($horo=="05/11"){$h = $h7;}
 if($horo=="06/11"){$h = $h7;}
 if($horo=="07/11"){$h = $h7;}
 if($horo=="08/11"){$h = $h7;}
 if($horo=="09/11"){$h = $h7;}
 if($horo=="10/11"){$h = $h7;}
 if($horo=="11/11"){$h = $h7;}
 if($horo=="12/11"){$h = $h7;}
 if($horo=="13/11"){$h = $h7;}
 if($horo=="14/11"){$h = $h7;}
 if($horo=="15/11"){$h = $h7;}
 if($horo=="16/11"){$h = $h7;}
 if($horo=="17/11"){$h = $h7;}
 if($horo=="18/11"){$h = $h7;}
 if($horo=="19/11"){$h = $h7;}
 if($horo=="20/11"){$h = $h7;}
 if($horo=="21/11"){$h = $h7;}
 // Sagitario
 if($horo=="22/11"){$h = $h8;}
 if($horo=="23/11"){$h = $h8;}
 if($horo=="24/11"){$h = $h8;}
 if($horo=="25/11"){$h = $h8;}
 if($horo=="26/11"){$h = $h8;}
 if($horo=="27/11"){$h = $h8;}
 if($horo=="28/11"){$h = $h8;}
 if($horo=="29/11"){$h = $h8;}
 if($horo=="30/11"){$h = $h8;}
 if($horo=="31/11"){$h = $h8;}
 if($horo=="01/12"){$h = $h8;}
 if($horo=="02/12"){$h = $h8;}
 if($horo=="03/12"){$h = $h8;}
 if($horo=="04/12"){$h = $h8;}
 if($horo=="05/12"){$h = $h8;}
 if($horo=="06/12"){$h = $h8;}
 if($horo=="07/12"){$h = $h8;}
 if($horo=="08/12"){$h = $h8;}
 if($horo=="09/12"){$h = $h8;}
 if($horo=="10/12"){$h = $h8;}
 if($horo=="11/12"){$h = $h8;}
 if($horo=="12/12"){$h = $h8;}
 if($horo=="13/12"){$h = $h8;}
 if($horo=="14/12"){$h = $h8;}
 if($horo=="15/12"){$h = $h8;}
 if($horo=="16/12"){$h = $h8;}
 if($horo=="17/12"){$h = $h8;}
 if($horo=="18/12"){$h = $h8;}
 if($horo=="19/12"){$h = $h8;}
 if($horo=="20/12"){$h = $h8;}
 if($horo=="21/12"){$h = $h8;}
 // Capricornio
 if($horo=="22/12"){$h = $h9;}
 if($horo=="23/12"){$h = $h9;}
 if($horo=="24/12"){$h = $h9;}
 if($horo=="25/12"){$h = $h9;}
 if($horo=="26/12"){$h = $h9;}
 if($horo=="27/12"){$h = $h9;}
 if($horo=="28/12"){$h = $h9;}
 if($horo=="29/12"){$h = $h9;}
 if($horo=="30/12"){$h = $h9;}
 if($horo=="31/12"){$h = $h9;}
 if($horo=="01/01"){$h = $h9;}
 if($horo=="02/01"){$h = $h9;}
 if($horo=="03/01"){$h = $h9;}
 if($horo=="04/01"){$h = $h9;}
 if($horo=="05/01"){$h = $h9;}
 if($horo=="06/01"){$h = $h9;}
 if($horo=="07/01"){$h = $h9;}
 if($horo=="08/01"){$h = $h9;}
 if($horo=="09/01"){$h = $h9;}
 if($horo=="10/01"){$h = $h9;}
 if($horo=="11/01"){$h = $h9;}
 if($horo=="12/01"){$h = $h9;}
 if($horo=="13/01"){$h = $h9;}
 if($horo=="14/01"){$h = $h9;}
 if($horo=="15/01"){$h = $h9;}
 if($horo=="16/01"){$h = $h9;}
 if($horo=="17/01"){$h = $h9;}
 if($horo=="18/01"){$h = $h9;}
 if($horo=="19/01"){$h = $h9;}
 if($horo=="20/01"){$h = $h9;}
 if($horo=="21/01"){$h = $h9;}
 // Acuario
 if($horo=="22/01"){$h = $h10;}
 if($horo=="23/01"){$h = $h10;}
 if($horo=="24/01"){$h = $h10;}
 if($horo=="25/01"){$h = $h10;}
 if($horo=="26/01"){$h = $h10;}
 if($horo=="27/01"){$h = $h10;}
 if($horo=="28/01"){$h = $h10;}
 if($horo=="29/01"){$h = $h10;}
 if($horo=="30/01"){$h = $h10;}
 if($horo=="31/01"){$h = $h10;}
 if($horo=="01/02"){$h = $h10;}
 if($horo=="02/02"){$h = $h10;}
 if($horo=="03/02"){$h = $h10;}
 if($horo=="04/02"){$h = $h10;}
 if($horo=="05/02"){$h = $h10;}
 if($horo=="06/02"){$h = $h10;}
 if($horo=="07/02"){$h = $h10;}
 if($horo=="08/02"){$h = $h10;}
 if($horo=="09/02"){$h = $h10;}
 if($horo=="10/02"){$h = $h10;}
 if($horo=="11/02"){$h = $h10;}
 if($horo=="12/02"){$h = $h10;}
 if($horo=="13/02"){$h = $h10;}
 if($horo=="14/02"){$h = $h10;}
 if($horo=="15/02"){$h = $h10;}
 if($horo=="16/02"){$h = $h10;}
 if($horo=="17/02"){$h = $h10;}
 if($horo=="18/02"){$h = $h10;}
 if($horo=="19/02"){$h = $h10;}
 if($horo=="20/02"){$h = $h10;}
 if($horo=="21/02"){$h = $h10;}
 // Piscis
 if($horo=="22/02"){$h = $h11;}
 if($horo=="23/02"){$h = $h11;}
 if($horo=="24/02"){$h = $h11;}
 if($horo=="25/02"){$h = $h11;}
 if($horo=="26/02"){$h = $h11;}
 if($horo=="27/02"){$h = $h11;}
 if($horo=="28/02"){$h = $h11;}
 if($horo=="29/02"){$h = $h11;}
 if($horo=="30/02"){$h = $h11;}
 if($horo=="31/02"){$h = $h11;}
 if($horo=="01/03"){$h = $h11;}
 if($horo=="02/03"){$h = $h11;}
 if($horo=="03/03"){$h = $h11;}
 if($horo=="04/03"){$h = $h11;}
 if($horo=="05/03"){$h = $h11;}
 if($horo=="06/03"){$h = $h11;}
 if($horo=="07/03"){$h = $h11;}
 if($horo=="08/03"){$h = $h11;}
 if($horo=="09/03"){$h = $h11;}
 if($horo=="10/03"){$h = $h11;}
 if($horo=="11/03"){$h = $h11;}
 if($horo=="12/03"){$h = $h11;}
 if($horo=="13/03"){$h = $h11;}
 if($horo=="14/03"){$h = $h11;}
 if($horo=="15/03"){$h = $h11;}
 if($horo=="16/03"){$h = $h11;}
 if($horo=="17/03"){$h = $h11;}
 if($horo=="18/03"){$h = $h11;}
 if($horo=="19/03"){$h = $h11;}
 if($horo=="20/03"){$h = $h11;}
 if($horo=="21/03"){$h = $h11;}
 return $h;
}
?>

En este caso recojo y divido el string de la fecha $date que siempre estará en formato "Y-m--d". Se que febrero no tiene 31 días :D pero como es todo un copy/paste no me moleste en borrar los días sobrantes. De esta manera comparo el string recogido con los strings del if. Esta fue mi única solución que ha funcionado sin problemas.

Un saludo.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas