Home > Mobile >  PHP check if date is expired
PHP check if date is expired

Time:10-28

i have an Array with this string value.

[VALIDUNTIL] => 28.08.23

Now i need to compare it with the todays date and check if the VALIDUNTIL date is in the same month and year as the today´s date i looked for a while now but i cant find any solution.

while ($row = oci_fetch_assoc($dbResult)) 
{
    $data[] = $row;
}


foreach($data as $ar) {
    if ($ar['VALIDUNTIL'] && $ar['VALIDUNTIL'] != null) {   
        if(strtotime($ar['VALIDUNTIL']) >= strtotime($date)) {
           
        }   
    }

When i try to do it with timestamps it cant read the VALIDUNTIL date and gives me the 1970 timestamp strtotime seems not to work too. tnx 4 help.

CodePudding user response:

while ($row = oci_fetch_assoc($dbResult)) 
{
    if(!empty($row['VALIDUNTIL']) && !is_null($row['VALIDUNTIL'])){
       $valid_date = date('d-m-Y', strtotime($row['VALIDUNTIL']));
       $current_date = date('d-m-Y');
       if($valid_date >= $current_date ) {
          echo "True";
       }   
    }
}

CodePudding user response:

Here are the PHP string conversions for time ref. https://www.w3schools.com/php/php_ref_date.asp

  echo(strtotime("now") . "<br>");
  echo(strtotime("3 October 2005") . "<br>");
  echo(strtotime(" 5 hours") . "<br>");
  echo(strtotime(" 1 week") . "<br>");
  echo(strtotime(" 1 week 3 days 7 hours 5 seconds") . "<br>");
  echo(strtotime("next Monday") . "");
  echo(strtotime("last Sunday"));

You'd need to have both dates in the same format. The above is from : https://www.w3schools.com/php/func_date_strtotime.asp#:~:text=The strtotime() function parses,are mapped to 1970-2000.

if you echo (strtotime($ar['VALIDUNTIL']) and strtotime($date)) back to the screen then you can visually compare and check to see the format which you are using is the same for both dates and adjust accordingly.

  • Related