Jump to content

[SOLVED] Conditional testing


Tboy_forum

Recommended Posts

Hi all

Yep, I'm a noob so go lightly please ;D ;D

 

Can someone please assist me with finding the conditional problem here. Its part functioning ??? I.O.W. - I'm not sure where the conditional problem lies. It executes the first part of the code. That is, it does assign values to $year, $month and $day but the condition is wrong - it executes whether the string length is 6 or 5 characters :'(

 

ANY help is appreciated

 

		   if (trim(strlen($user['user_birthday'])) != 6 )
		   {
                     $year 	= substr($user['user_birthday'], 4, 4);
		   $month 	= substr($user['user_birthday'], 0, 2);
		   $day 	= substr($user['user_birthday'], 2, 2);   			   
                      }                                                
                      else if (trim(strlen($user['user_birthday'])) != 5 )
		   {

 

			if ($user['user_birthday'] AND $user['user_birthday'] != 999999)
		{
		   if (trim(strlen($user['user_birthday'])) != 6 )
		   {
                     $year 	= substr($user['user_birthday'], 4, 4);
		   $month 	= substr($user['user_birthday'], 0, 2);
		   $day 	= substr($user['user_birthday'], 2, 2);   			   
                      }                                                
                      else if (trim(strlen($user['user_birthday'])) != 5 )
		   {
                     $year 	= substr($user['user_birthday'], 3, 4);
		   $month 	= substr($user['user_birthday'], 0, 1);
		   $day 	= substr($user['user_birthday'], 1, 2);   			   
                      }  
		   $try->set_value('nonmandatory', 'birthday',			"{$month}-{$day}-{$year}");
		   $try->set_value('nonmandatory', 'birthday_search',	"{$year}-{$month}-{$day}");
		}

Link to comment
Share on other sites

really? didn't know that way worked...

 

look at that...straight from the PHP site: "In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word)."

Link to comment
Share on other sites

really? didn't know that way worked...

 

look at that...straight from the PHP site: "In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word)."

LOL. I didn't know that elseif worked. We both learned something.

Link to comment
Share on other sites

the != needs to be ==

 

try this code:

<?php
  if ($user['user_birthday'] && $user['user_birthday'] != 999999)
  {
    $bday = trim($user['user_birthday']);
    if (strlen($bday) == 6 )
    {
      $year  = substr($bday, 4, 4);
      $month = substr($bday, 0, 2);
      $day   = substr($bday, 2, 2);            
    }
    else if (strlen($bday) == 5 )
    {
      $year  = substr($user['user_birthday'], 3, 4);
      $month = substr($user['user_birthday'], 0, 1);
      $day   = substr($user['user_birthday'], 1, 2);            
    }
    else
    {
      die("Invalid Bday");
    }  
    $try->set_value('nonmandatory', 'birthday',      "{$month}-{$day}-{$year}");
    $try->set_value('nonmandatory', 'birthday_search', "{$year}-{$month}-{$day}");
  }
?>

Link to comment
Share on other sites

Don't use trim. You shouldn't need it.

And don't use AND. Use && instead.

 

Try this:

 

<?php
if ( strlen($user['user_birthday']) != 6 )
{
$year   = substr($user['user_birthday'], 4, 4);
$month 	= substr($user['user_birthday'], 0, 2);
$day 	= substr($user['user_birthday'], 2, 2);   			   
}                                                
elseif ( strlen($user['user_birthday']) != 5 )
{

if ($user['user_birthday'] && $user['user_birthday'] != 999999)
{
	if ( strlen($user['user_birthday']) != 6 )
	{
                	$year  = substr($user['user_birthday'], 4, 4);
	        $month = substr($user['user_birthday'], 0, 2);
		$day   = substr($user['user_birthday'], 2, 2);   			   
                }                                                
                elseif ( strlen($user['user_birthday']) != 5 )
	{
                     	$year  = substr($user['user_birthday'], 3, 4);
	     	$month = substr($user['user_birthday'], 0, 1);
 	    	$day = substr($user['user_birthday'], 1, 2);   			   
                }  

	$try->set_value('nonmandatory', 'birthday', "{$month}-{$day}-{$year}");
	$try->set_value('nonmandatory', 'birthday_search', "{$year}-{$month}-{$day}");
}
?>

Link to comment
Share on other sites

Thanks for the assistance

 

I am getting the following results

 

5 digit dates are returning:  10-61-962 instead of 01-06-1962. So it would seem the condition for 5 digits is not being met???

 

And 6 digit dates are returning:  11-28-1978 - which is correct.

 

Its only the 5 digit dates which are giving me nightmares

Link to comment
Share on other sites

omg...how did i miss that before...the lengths are 7 and 8, not 5 and 6 (at least with your examples)

 

<?php
  if ($user['user_birthday'] && $user['user_birthday'] != 999999)
  {
    if (strlen($user['user_birthday']) == 8 )
    {
      $year  = substr($user['user_birthday'], 4, 4);
      $month = substr($user['user_birthday'], 0, 2);
      $day   = substr($user['user_birthday'], 2, 2);            
    }
    else if (strlen($user['user_birthday']) == 7 )
    {
      $year  = substr($user['user_birthday'], 3, 4);
      $month = substr($user['user_birthday'], 0, 1);
      $day   = substr($user['user_birthday'], 1, 2);            
    }
    else
    {
      die("Invalid Bday");
    }  
    $try->set_value('nonmandatory', 'birthday',      "{$month}-{$day}-{$year}");
    $try->set_value('nonmandatory', 'birthday_search', "{$year}-{$month}-{$day}");
  }
?>

1061962 => 1-06-1962
11281978 => 11-28-1978

Link to comment
Share on other sites

The origin info is either:

 

1061962 - 5 digits outputs 1-06-1962  if checking for 5 digits is first

 

        12251965 - 6 digits outputs 1-12-8197  and 6 digits is second

 

What I have found is that whatever test condition is first, that is then the resulting output. I.O.W. the second condition is not being executed???

 

 

Either the one condition is correct on the output - never both

 

Link to comment
Share on other sites

omg...how did i miss that before...the lengths are 7 and 8, not 5 and 6 (at least with your examples)

 

 

You sir, are a genius  ;D ;D ;D ;D ;D ;D ;D ;D

 

I must have been dreaming!!

 

I look at the problem and don't even see it - thanks a STACK for spotting that one - I thought I was going nuts ;D

 

Many thanks for all the assistance here, I really do appreciate it

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.