Chewy_Louie Posted September 15, 2016 Share Posted September 15, 2016 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/302192-simple-if-var-var-help-newbie/ Share on other sites More sharing options...
benanamen Posted September 15, 2016 Share Posted September 15, 2016 You are improperly storing the datetime. Format should be YYYY-MM-DD HH:MM:SS Quote Link to comment https://forums.phpfreaks.com/topic/302192-simple-if-var-var-help-newbie/#findComment-1537566 Share on other sites More sharing options...
Chewy_Louie Posted September 15, 2016 Author Share Posted September 15, 2016 (edited) 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 September 15, 2016 by Chewy_Louie Quote Link to comment https://forums.phpfreaks.com/topic/302192-simple-if-var-var-help-newbie/#findComment-1537567 Share on other sites More sharing options...
Yohanne Posted September 15, 2016 Share Posted September 15, 2016 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; } Quote Link to comment https://forums.phpfreaks.com/topic/302192-simple-if-var-var-help-newbie/#findComment-1537569 Share on other sites More sharing options...
Chewy_Louie Posted September 15, 2016 Author Share Posted September 15, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302192-simple-if-var-var-help-newbie/#findComment-1537573 Share on other sites More sharing options...
Chewy_Louie Posted September 15, 2016 Author Share Posted September 15, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302192-simple-if-var-var-help-newbie/#findComment-1537574 Share on other sites More sharing options...
Jacques1 Posted September 15, 2016 Share Posted September 15, 2016 (edited) 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 September 15, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/302192-simple-if-var-var-help-newbie/#findComment-1537575 Share on other sites More sharing options...
Chewy_Louie Posted September 16, 2016 Author Share Posted September 16, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302192-simple-if-var-var-help-newbie/#findComment-1537635 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.