StefanRSA Posted July 15, 2010 Share Posted July 15, 2010 I have the following function... function daysDifference($endDate, $beginDate) { //explode the date by "-" and storing to array $date_parts1=explode("-", $beginDate); $date_parts2=explode("-", $endDate); //gregoriantojd() Converts a Gregorian date to Julian Day Count $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]); $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]); return $end_date - $start_date; } function bumpup(){ $diff=daysDifference($now,$addate); if( $diff >='30'){ echo 'More than 30 days'; } else { echo 'Less than 30 Days'; } } $now=date("Y-m-d H:i:s"); $addate='2010-01-27 13:59:13'; bumpup(); echo '<br> ----------------------------- <br>'; $now=date("Y-m-d H:i:s"); $addate='2010-07-27 13:59:13'; bumpup(); Why does it echo Less than 30 Days ----------------------------- Less than 30 Days Was thinking it should echo More than 30 Days ----------------------------- Less than 30 Days Quote Link to comment https://forums.phpfreaks.com/topic/207820-simple-function-not-working-nicely/ Share on other sites More sharing options...
Adam Posted July 15, 2010 Share Posted July 15, 2010 Variable scope. Once inside the bumpup() function, $now and $addate do not exist. You need to pass them as parameters. Quote Link to comment https://forums.phpfreaks.com/topic/207820-simple-function-not-working-nicely/#findComment-1086369 Share on other sites More sharing options...
StefanRSA Posted July 15, 2010 Author Share Posted July 15, 2010 HUH??? Sorry Mr Adam... Thanks for the reply. I am not sure what you mean? Where should I then pass $addate and $now as parameter? Oh, and its only $addate that will differ in a while loop. (Hope it makes sense) Quote Link to comment https://forums.phpfreaks.com/topic/207820-simple-function-not-working-nicely/#findComment-1086381 Share on other sites More sharing options...
Adam Posted July 15, 2010 Share Posted July 15, 2010 It doesn't matter what differs where, within the function the variables are out of scope (the function is unable to see them). First modify the function to accept 2 parameters: function bumpup($now, $addate){ Then call the function, passing the 2 variables: $now=date("Y-m-d H:i:s"); $addate='2010-01-27 13:59:13'; bumpup($now, $addate); // pass the values here echo '<br> ----------------------------- <br>'; $now=date("Y-m-d H:i:s"); $addate='2010-07-27 13:59:13'; bumpup($now, $addate); // pass the values here Quote Link to comment https://forums.phpfreaks.com/topic/207820-simple-function-not-working-nicely/#findComment-1086387 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 this should fix your problem. the other will also work. all i did was remove the vars you stored the data in and sent the data directly to the function instead of sending the to the var and the sending the var to the function. you needed to have it send the data to the function. the then function grabs that data and uses it. in your code the vars was defined in the script but not in that specific function. function bumpup($now,$addate){ $diff=daysDifference($now,$addate); if( $diff >='30'){ echo 'More than 30 days'; } else { echo 'Less than 30 Days'; } } bumpup(date("Y-m-d H:i:s") , '2010-01-27 13:59:13'); echo '<br> ----------------------------- <br>'; bumpup(date("Y-m-d H:i:s") , '2010-07-27 13:59:13'); Quote Link to comment https://forums.phpfreaks.com/topic/207820-simple-function-not-working-nicely/#findComment-1086391 Share on other sites More sharing options...
StefanRSA Posted July 15, 2010 Author Share Posted July 15, 2010 Thanks! You guys are GREAT! I am going to try this right now... Will keep you posted! Quote Link to comment https://forums.phpfreaks.com/topic/207820-simple-function-not-working-nicely/#findComment-1086407 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 So has the code worked? Quote Link to comment https://forums.phpfreaks.com/topic/207820-simple-function-not-working-nicely/#findComment-1086442 Share on other sites More sharing options...
StefanRSA Posted July 15, 2010 Author Share Posted July 15, 2010 Yip! 100% Thanks Guys! Quote Link to comment https://forums.phpfreaks.com/topic/207820-simple-function-not-working-nicely/#findComment-1086450 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 Your welcome, now off to help more people with php coding lol. Also this is a good learning experience if you ever want learn in a different way. You get to help other people with coding, learn from both people mistakes, and have the code for future reference when you are coding. Quote Link to comment https://forums.phpfreaks.com/topic/207820-simple-function-not-working-nicely/#findComment-1086452 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.