PHP: If Result with Function

Asked

Viewed 198 times

-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 @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")))) { 

Upshot;

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

PS: I changed the code to but I am having trouble showing the result. If the result is 10 above the current date 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>' ;}
  • 3

    A space is missing before "days". If not, maybe the date format that comes from your bank is not interpretable by strtotime.

  • Good morning, what’s the problem? please exchange $Row[xx] for real dates because the problem might be the formatted date.

  • What date format are you getting in $Row[11...] ?

  • Dates come in the format: Year-Month-Day (0000-00-00 )

  • Even with the hard space not working

  • Can do var_dump($row); and ask the question to clarify your code and we can help?

  • 1- Place the validation condition (check for empty dates) within your function estaParaExpirar. 2- Why lightning you need to compare 7 dates of a database row? Probably your table design is very wrong.

  • if any of the 7 dates are missing less than 10 days send a message. It will be the email content.

  • Why are you wearing and and && instead of || (or) on duty? In this case your if requires all conditions to be met.

  • I am trying to make Row[?? ] different from 0000-00-00 and 10 days to complete the condition. And that for all others

Show 5 more comments

2 answers

1

Assuming I understand your problem, I have an alternative solution based on the class Datetime

Its problem seems to revolve around a comparison between the current date and a future date, ie a difference of intervals that, with the Datetime is resolved through Datetime::diff():

$currentDate = new DateTime;

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

Simple as that! For a date 10 days in the future we will have in $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 above output was generated based on a simple strtotime('+10 days'), hence the input d seems wrong to show 9, but that’s because the hours, minutes and seconds were not informed.

Its biggest problem, however, seems to be regarding the display. And, unfortunately, the Datetime It makes things a little difficult because there are no negative numbers for her. Instead, when the difference computed is prior to the (current) base date the input invert shall be changed from zero to one.

And that’s what you’d work your parole officers with:

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 the flag invert is zero, it means that the difference is positive, that is, the future date is really future. Hence it is a simple comparison: If the number of days is greater than the 10 you set, we show that X days to go. otherwise we show that the expires in X days.

Case to flag invert be 1, we have that the future date has passed, so we just show that expired x days ago.

As I do not know where this will be used, depending on the case you will not show that has already run out the deadline, getting this Else optional or for use other than in the form 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 demonstration:

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

0

Your function is not validating the input date, 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. 
  • I think I know what may be causing this error, are the dates that have no results or that have the date with the value of 0000-00-00

  • In the condition I have to put something "if ($Row[11] != '0000-00-00' and (estaParaExpirar($Row[11],"10")) || ($Row[12] != '0000-00-00' and (estaParaExpirar($Row[12],"10")) || " Correct?

  • Fix the function to return false if it is zero or null

  • With null Works. The problem is with dates of 0000-00-00

  • I edited the answer, see how was the function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.