开发者

in php i need one line if condition for time compare

开发者 https://www.devze.com 2022-12-18 23:10 出处:网络
i have to value $mo=strtotime($input_array[\'MondayOpen\']); $mc=strtotime($input_array[\'MondayClose\']);

i have to value

 $mo=strtotime($input_array['MondayOpen']);
 $mc=strtotime($input_array['MondayClose']);

now i need a if condition to display an error on below conditions

  1. if one of them($mo or $mc) are empty, null or blank.
  2. if close time($mc) is less than open time($mo)

means if both are empty(null) or $mc>$mo then go further

please suggest optimized one line if condition for this

i know it seems very basic questi开发者_如何学Pythonon, but i m facing problem when both are null either i was using simple

if(($mo==NULL && $mc!=NULL) || ( $mo>=$mc && ($mo!=NULL && $mc!=NULL))  )


Keep in mind that 0, null, and blank all mean completely different things here. As indicated previously, strtotime will never return NULL. However, 0 is a valid unix timestamp, whereas false means that the strtotime function was unable to process the value provided.

Also, you've requested that a single-line solution; however, in my opinion, it is much better in this case to write out each condition and display a different error message for each condition. That way, the user knows what actually went wrong. Perhaps this is a better way:

// Only check for errors if we have at least one value set
if (!empty($input['MondayOpen']) || !empty($input['MondayClosed']) {
    $mo = strtotime($input['MondayOpen']);
    $mc = strtotime($input['MondayClosed']);

    $invalid = false;
    if (false === $mo) {
        echo "Invalid Opening Time\n";
        $invalid = true;
    }

    if (false === $mc) {
        echo "Invalid Closing Time\n";
        $invalid = true;
    }

    if (!$invalid && $mc <= $mo) {
        echo "Closing time must be After Opening Time\n";
        $invalid = true;
    }

    if ($invalid) {
        exit();  // Or handle errors more gracefully
    }
}

// Do something useful


All right. How about this.

It checks whether $mo and $mc are valid dates using is_numeric. Any NULL or false values will be caught by that. I haven't tested it but it should work.

I spread it into a huge block of code. In the beginning, when learning the language, this is the best way to make sense out of the code. It is not the most elegant, nor by far the shortest solution. Later, you can shorten it by removing whitespace, or by introducing or and stuff.

I'm not 100% sure about the number comparison part, and I don't have the time to check it right now. You'll have to try out whether it works.

You need to decide how you want to handle errors and insert the code to where my comments are. A simple echo might already do.

// If $mo or $mc are false, show error. 
// Else, proceed to checking whether $mo is larger
// than $mc.

if ((!is_numeric($mo)) and (is_numeric($mc)))
 {
   // Error: $mo is either NULL, or false, or something else, but not a number.
   // While $mc IS a number.
 }
elseif ((!is_numeric($mc)) and (is_numeric($mo)))
 {
   // Error: $mc is either NULL, or false, or something else, but not a number.
   // While $mo IS a number. 
 }
else
 {

   if (($mc <= $mo) and ((is_numeric($mc) or (is_numeric($mo)))))
    {
       // Error: closing time is before opening time.
    }
    else
     {
       // Success!!

      }

 }


in php, strotime will return a integer or false. Checking for null in this case will never bear fruit, but otherwise...

if((!$mo xor !$mc) || ($mc && $mc<=$mo)){
  print('error');
}else{
 print('no_error');
}

oops, edited for correctness. I transposed $mc and $mo. XOR should be correct though.


You can try:

print ((empty($mo) && empty($mc)) || ($mc > $mo)) ? 'case_true_message' : 'case_false_message';

But you should also check the manual :) - for basic control structures

0

精彩评论

暂无评论...
验证码 换一张
取 消