Jump to content

Simple If var == var Help. (Newbie)


Chewy_Louie

Recommended Posts

Ok guys im new to php taught myself years back, and now decided to build a script that i needed so really rusty and just learning and remembering as i go along.

 

Ok basically I'm having trouble seeing if a varible = value in a if statement. See code below.

 

Basically im using the explode function to parse => $row['timedate'] witch always looks something like this =>

"14:02:30 Aug 20, 2016" and im  putting in the values into the $date_array varible.

 

Then im adding the value of $date_array[2] to $str_month which now holds the month. Which in this case would be "Aug"

 

it then goes to the if statements and should assign the the text month value to its numeric value, but its just not doing it. What im i doing wrong? I just cant seem to get this to work! It should be fairly simple but im lost.

 

Looking forward to hearing back.

 

Thanks.

  $date_array = explode(" ",$row['timedate']);

           $str_month =  $date_array[2];
            
            if ($str_month == 'Jan') {
                $str_month = '1';
            }
    
    
            if ($str_month == 'Feb') {
                $str_month = '2';
            }   
    
    
           if ($str_month == 'Mar') {
               $str_month = '3';
           }
    
           if ($str_month == 'Apr') {
               $str_month = '4';
           }
    
           if ($str_month == 'May') {
              $str_month = '5';
           }
    
    
          if ($str_month == 'June') {
            $str_month = '6';
          }
    
    if ($str_month == 'July') {
        $str_month = '7';
    }
    
    
    if ($str_month == 'Sept') {
        $str_month = '9';
        echo "its sept";
    }
    
    
    if ($str_month == 'Oct') {
        $str_month = '10';
    }
    
    
    if ($str_month == 'Nov') {
       $str_month = '11';
    }
    
    
    if ($str_month == 'Dec') {
        $str_month = '12';
    } 
            
            
Link to comment
Share on other sites

You are improperly storing the datetime. Format should be YYYY-MM-DD HH:MM:SS

Thanks for the reply! I'm working with the PayPal ipn notification api. They give me that datetime format and i just store it in the row in my database i named "timedate" all i need to do is get the "Aug" out of the value, and assign the number value to the month. which in this case $str_month would end up = 9 after going through the if statements, but its just not finding a match, and $str_month ends up still being "Aug" even after going through all the if statements.

Edited by Chewy_Louie
Link to comment
Share on other sites

hope it could give you an idea.

                if(date( 'M') == 'Jan' || date( 'M') == 1)
			{ echo "month"; }
		elseif(date( 'M') == 'Feb' || date( 'M') == 2)	
			{ echo "month"; }
		elseif(date( 'M') == 'Mar' || date( 'M') == 3)	
			{ echo "month"; }
		elseif(date( 'M') == 'Apr' || date( 'M') == 4)	
			{ echo "month"; } 
		elseif(date( 'M') == 'May' || date( 'M') == 5)	
			{ echo "month"; }
		elseif(date( 'M') == 'Sep' || date( 'M') == 9)	
			{ echo "month"; }
		else 
			{
				return false;
			}
Link to comment
Share on other sites

 

hope it could give you an idea.

                if(date( 'M') == 'Jan' || date( 'M') == 1)
			{ echo "month"; }
		elseif(date( 'M') == 'Feb' || date( 'M') == 2)	
			{ echo "month"; }
		elseif(date( 'M') == 'Mar' || date( 'M') == 3)	
			{ echo "month"; }
		elseif(date( 'M') == 'Apr' || date( 'M') == 4)	
			{ echo "month"; } 
		elseif(date( 'M') == 'May' || date( 'M') == 5)	
			{ echo "month"; }
		elseif(date( 'M') == 'Sep' || date( 'M') == 9)	
			{ echo "month"; }
		else 
			{
				return false;
			}

Thanks. Gonna try that way of using my if statements. then hopefully solve the problem. If not will be back lol

Link to comment
Share on other sites

Thanks for the reply! I'm working with the PayPal ipn notification api. They give me that datetime format and i just store it in the row in my database i named "timedate" all i need to do is get the "Aug" out of the value, and assign the number value to the month. which in this case $str_month would end up = 8 after going through the if statements, but its just not finding a match, and $str_month ends up still being "Aug" even after going through all the if statements.

 

August would be not 8 not 9 .Corrected above

Link to comment
Share on other sites

Fumbling with your own date calculation functions doesn't make any sense, because PHP – like every serious programming language in existence – is perfectly capable of doing that itself.

 

And storing timestamps as arbitrary text in the database is plain wrong, as benanamen already said. You use the DATETIME type. If PayPal doesn't provide the right format, you simply parse the input (using PHP's standard library) and then reformat it.

 

So a sane approach to the problem would be

<?php

// the timestamp formats as used by PayPal and MySQL
const PAYPAL_TIMESTAMP_FORMAT = 'G:i:s M j, Y';
const MYSQL_TIMESTAMP_FORMAT = 'Y-m-d G:i:s';

// the input from PayPal
$rawNotificationTimestamp = '14:02:30 Aug 20, 2016';

// the parsed timestamp
$notificationTimestamp = DateTime::createFromFormat(PAYPAL_TIMESTAMP_FORMAT, $rawNotificationTimestamp);

// reformat timestamp for MySQL
$dbNotificationTimestamp = $notificationTimestamp->format(MYSQL_TIMESTAMP_FORMAT);
var_dump($dbNotificationTimestamp);

// get month
$notificationMonth = (int) $notificationTimestamp->format('n');
var_dump($notificationMonth);

Work with the programming language, not against it.

Edited by Jacques1
Link to comment
Share on other sites

Fumbling with your own date calculation functions doesn't make any sense, because PHP – like every serious programming language in existence – is perfectly capable of doing that itself.

 

And storing timestamps as arbitrary text in the database is plain wrong, as benanamen already said. You use the DATETIME type. If PayPal doesn't provide the right format, you simply parse the input (using PHP's standard library) and then reformat it.

 

So a sane approach to the problem would be

<?php

// the timestamp formats as used by PayPal and MySQL
const PAYPAL_TIMESTAMP_FORMAT = 'G:i:s M j, Y';
const MYSQL_TIMESTAMP_FORMAT = 'Y-m-d G:i:s';

// the input from PayPal
$rawNotificationTimestamp = '14:02:30 Aug 20, 2016';

// the parsed timestamp
$notificationTimestamp = DateTime::createFromFormat(PAYPAL_TIMESTAMP_FORMAT, $rawNotificationTimestamp);

// reformat timestamp for MySQL
$dbNotificationTimestamp = $notificationTimestamp->format(MYSQL_TIMESTAMP_FORMAT);
var_dump($dbNotificationTimestamp);

// get month
$notificationMonth = (int) $notificationTimestamp->format('n');
var_dump($notificationMonth);

Work with the programming language, not against it.

Thanks for your input. I'm not a serious programmer, so my code to you professionals like you may like offensive or down right disgusting (I'v been told that before on other forums) lol.

 

I just create desktop programs, and web programs as i need them for self use and they do what i need them to do (PHP been about 14 years since i messed with it so really really rusty). btw looking at your code, im sure i can pick up some stuff from it and some google searching as well.

 

Thanks.

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.