tags:

views:

253

answers:

4

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 question, 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))  )
+2  A: 

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.

Mike Sherov
it's always display error on every condition
diEcho
fixed it. I believe :-)
Mike Sherov
Still display Error when both fields have no value...please first u check on ur side
diEcho
Thank you, now it's working perfect
diEcho
A: 

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

andreas
its does't display error when only $mc is full but $mo is empty
diEcho
+6  A: 

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
Brian Lacy
very expressive answer....thank you sir. i was really scared when so many people behave so rudely when i post so basic question..actaully i was puzzled after converting into strtotime ans then comparing
diEcho
it disply invalid opening time if we remain both field blank
diEcho
this can't be right though?? he wanted to continue (not give an error) if BOTH were false!
Mike Sherov
Also, although 0 is a valid unix timestamp and it's important to illustrate the weakly typed pitfall, is it really necessary to check for it here?
Mike Sherov
@Mike Sherov yes sir it is necessary to check....is there any other method to check.. bcoz after this validation if everything is fine then i apply `date()` on `$mo` and `$mt` , and if there is 0 in field then date() convert it to some value. that's y i need to check these all conditions
diEcho
no, what I mean is that you're not going to pass a VALID 0. You don't need === unless you plan on checking for VALID 0's.
Mike Sherov
I've modified my answer to offer a more robust solution, since you were both complaining. ;)
Brian Lacy
Mike, I understand your point, but I refuse to offer a solution that makes lazy assumptions about user input. There's nothing in the Question that demands validation about the valid date range; if there were, I would treat that as a separate validation criteria. In practice, I myself have made such assumptions, but I prefer to avoid it when it's as convenient as adding an extra '='. :)
Brian Lacy
Final Note to the Asker:All of the changes I made in my most recent edit really should be pretty straightforward to figure out just by thinking through it. As I said before, I'm willing to help you get in the mode, but I agree with the general sentiment that we really shouldn't have to write all your code for you! :) Just a thought.
Brian Lacy
+3  A: 

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!!

      }

 }
Pekka
diEcho
@I like PHP, why on earth do you need a single line statement when you're only learning the language? And it should do exactly as you say in your question: If $mo *or* $mc are null, you will get an error. When both are `0`, then show no error but silently adjust. If you mean blank instead of 0, replace `0` by `''`.
Pekka
@Pekka ... I SAID when both are null/blank/false, then it doesn't show any error. does strtotime() ever return `0`? i request you sir,please read my question again that on which condition i need to display errors(only 2 condtions)..i heard that one if execute more fast than if inside if takes time, thats why i asked for one line code, if i m wrong then it will be fine if you code runs perfectly.thank you...i don't know why people give negative point on this question
diEcho
@I Like PHP, forget about speed optimization in `if` statements. They are totally useless and unnecessary. You need to work with clean, readable code. Now for the second part, I admit I misread your question. I'm editing my answer, hang on.
Pekka
@I like PHP, should work now.
Pekka
Oh and by the way, `strtotime()` will very well return `0` for January 1st, 1970, 0:00 UTC.
Pekka
Thank You Sir, now it's perfect, thank you very much.
diEcho
Oh and by the way, there is syntax error in `if (($mc <= $mo) and ((is_numeric($mc) or (is_numeric($mo)))`there would be two more paranthesis in last`if (($mc <= $mo) and ((is_numeric($mc) or (is_numeric($mo)))))`
diEcho
You're welcome. As I said, forget about optimizing for speed in these things. Optimization becomes really important when doing a lot of database operations or dealing with large amounts of data. A clean code structure is way more important than 3 milliseconds saved.
Pekka
@I like PHP: Re Syntax error: done, corrected.
Pekka