Jump to content

Simple function not working nicely???


StefanRSA

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/207820-simple-function-not-working-nicely/
Share on other sites

HUH???  :shy:

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)

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

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');

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.