PHP: If result with Function

-2

I'm having a little problem using If with a function to show a result.

function estaParaExpirar($data, $dias){
return(strtotime($data) < strtotime("+".$dias. "days") );
}


  if (estaParaExpirar($row[11], "10") ||
  estaParaExpirar($row[12], "10") ||  estaParaExpirar($row[13], "10") ||
  estaParaExpirar($row[14], "10") ||
  estaParaExpirar($row[15], "10") ||
  estaParaExpirar($row[16], "10") ||
  estaParaExpirar($row[17], "10")) {  
   $Nome1 = '<p>Nome: '.$row[10].'</p>' ;}

I did a little test with the code that @jader gave.

$row[11] = '0000-00-00';
$row[12] = '2014-08-15';
$row[13] = '2014-08-15';
$row[14] = '2014-08-15';
$row[15] = '2014-08-15';
$row[16] = '2014-08-15';
$row[17] = '2014-08-15';

if (($row[11] != '0000-00-00' and $row[12] != '0000-00-00' and $row[13] 
!= '0000-00-   00'      and $row[14] != '0000-00-00' and $row[15] != '0000-00-00'
and $row[16] != '0000-00-00' and $row[17] != '0000-00-00')
and ((estaParaExpirar($row[11], "10")) and (estaParaExpirar($row[12], "10")) and 
(estaParaExpirar($row[13], "10")) and
(estaParaExpirar($row[14], "10")) and   
(estaParaExpirar($row[15], "10")) and 
(estaParaExpirar($row[16], "10")) and
(estaParaExpirar($row[17], "10")))) { 

Result;

echo 'missing more than 10 days'; You're wrong

PS: I changed the code to but I'm having trouble displaying the result. If the result is 10 above the current date it shows no result

     if (($row[11] != '0000-00-00' && estaParaExpirar($row[11], "10")) 
    || ($row[12] != '0000-00-00' && estaParaExpirar($row[12], "10"))
    || ($row[13] != '0000-00-00' && estaParaExpirar($row[13], "10")) 
    || ($row[14] != '0000-00-00' && estaParaExpirar($row[14], "10")) 
    || ($row[15] != '0000-00-00' && estaParaExpirar($row[15], "10")) 
    || ($row[16] != '0000-00-00' && estaParaExpirar($row[16], "10")) 
    || ($row[17] != '0000-00-00' && estaParaExpirar($row[17], "10"))) {     
    $Nome1 = '<p>Nome: '.$row[10].'</p>' ;}
    
asked by anonymous 11.08.2014 / 13:56

2 answers

1

Assuming I understand your problem, I have a workaround based on the DateTime class

Your problem seems to revolve around a comparison between the current date and a future date, that is, a gap between the DateTime is resolved through DateTime :: diff () :

$currentDate = new DateTime;

$diff = $currentDate -> diff( new DateTime( $futureDate ) );

Simple like that! For a date 10 days in the future we will have $ diff :

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 9
    [h] => 7
    [i] => 14
    [s] => 29
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 11
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)
  

The output above was generated based on a simple strtotime ('+ 10 days'), so the input d seems wrong to show 9, but this is because the hours, minutes and seconds.

Your biggest problem, however, seems to be regarding the display. And, unfortunately, the DateTime makes things a bit difficult because there are no negative numbers for it. Instead, when the computed difference is prior to the current (current) date the invert entry is changed from zero to one.

And that's what you'd work with your conditionals:

if( $diff -> invert == 0 ) {

    if( $diff -> d > 10 ) {

        return sprintf( '%d days remaining', $diff -> d );

    } else {

        return sprintf( 'Will expire in %d days', $diff -> d );
    }

} else {

    return sprintf( 'Expired %d days ago', $diff -> d );
}

If flag invert is zero, it means that the difference is positive, ie the future date is actually future. That's a simple comparison: If the number of days is greater than the 10 you set, we show that X days are still missing . otherwise we show that the term expires in X days .

If the flag invert is 1, we have the future date passed, so we just show that has expired for X days . p>

As I do not know where this is going to be used, depending on the case you will not show that the deadline has expired, being this else optional or for use other than the one presented.

The complete code:

function showExpirationDate( $futureDate, $limit = 10 ) {

    $currentDate = new DateTime;

    $diff = $currentDate -> diff( new DateTime( $futureDate ) );

    if( $diff -> invert == 0 ) {

        if( $diff -> d > $limit ) {

            return sprintf( '%d days remaining', $diff -> d );

        } else {

            return sprintf( 'Will expire in %d days', $diff -> d );
        }

    } else {

        return sprintf( 'Expired %d days ago', $diff -> d );
    }
}

And the demo:

echo showExpirationDate( date( 'Y-m-d', strtotime( '+10 days' ) ) ); // Will expire in 9 days
    
12.08.2014 / 22:00
0

Your function is not validating the date of entry, I added the check to return false if the date is invalid or null and added the default value of days:

function estaParaExpirar($data, $dias=10) {
    if (!strtotime($data) || empty($data)) return false;
    return(strtotime($data) < strtotime("+".$dias. "days") );
}

// Hoje é 12/08/2014

$row[11] = '0000-00-00';

if (estaParaExpirar($row[11])) {
    echo 'esta para expirar em menos de 10 dias';
} else {
    echo 'faltam mais de 10 dias';
}

//retorno
// faltam mais de 10 dias

$row[11] = '2014-08-20';

if (estaParaExpirar($row[11])) {
    echo 'esta para expirar em menos de 10 dias';
} else {
    echo 'faltam mais de 10 dias';
}

//retorno
// esta para expirar em menos de 10 dias

Bonus a function that returns the days to expire:

function diasParaExpirar($data) {
    if (!strtotime($data) || empty($data)) return false;
    return floor( ( strtotime($data) - strtotime(date('Y-m-d')) ) / 60 / 60 / 24 );
}

// Hoje é 12/08/2014

$row[11] = '2014-08-20';

echo 'faltam: '.diasParaExpirar($row[11]).' dias para expirar.';

//retorno
// faltam: 8 dias para expirar. 
    
11.08.2014 / 21:10