This topic is locked

Calculation between 2 different dates

12/9/2012 8:29:43 PM
PHPRunner General questions
P
phpcmk author

Hi,
I have 2 fields (date1 and date2), and I am trying to calculate the number of days between these 2 fields. If date1 is empty field, I will get the current date.
Example: date1=Today date(eg. 15/12/2012), date2=User input (eg. 10/12/2012), difference=5
Below are my code which works if I put it in "Before record added" and "Before record updated":

----------------------------------------------------------

if ($values["date1"]=="" || $values["date1"]==NULL){

$date1 = time();

$date2 = strtotime($values["date2"]);

$date3 = $date1-$date2;

$values["difference"]= floor($date3/(606024));

}

-------------------------------------------------
However, I am now trying to increment the difference by 1 as each day goes by automatically after the intial record is created. Need someone advice on this.
Taking from the above example, on 15/12/2012, user created a record with date2=10/12/2012. The difference will be 5, however the following day, the difference will be automatically become 6, etc.
I tried putting the below code under "After successful login" but it seems not working.

------------------------------------------------

mysql_connect ("localhost", "username", "password") or die (mysql_error());
mysql_select_db ("database") or die (mysql_error());
$result=mysql_query ("Select from table_name");

$row=mysql_fetch_array($result);
if ($row["date1"]=="" || $row["date1"]==NULL){

$date1 = time();

$date2 = strtotime($row["date2"]);

$date3 = $date1-$date2;

$row["difference"]= floor($date3/(60
60*24));

}

------------------------------------------
Thanks in advance for the help.

C
cgphp 12/10/2012

I tried putting the below code under "After successful login" but it seems not working.

------------------------------------------------

mysql_connect ("localhost", "username", "password") or die (mysql_error());
mysql_select_db ("database") or die (mysql_error());
$result=mysql_query ("Select from table_name");

$row=mysql_fetch_array($result);
if ($row["date1"]=="" || $row["date1"]==NULL){

$date1 = time();

$date2 = strtotime($row["date2"]);

$date3 = $date1-$date2;

$row["difference"] = floor($date3/(60
60*24));

}


I don't see any increment by 1 in your code.

Why do you call the mysql_connect function? The database connection is active after the user login.
Check this solution:

$rs = CustomQuery("SELECT * FROM table_name");

while($row = db_fetch_array($rs))

{

if ($row["date1"] == "" || $row["date1"] == NULL)

{

$date1 = time();

$date2 = strtotime($row["date2"]);

$date3 = $date1-$date2;

$row["difference"]= floor($date3/(60*60*24));

//update here the difference field in the database

}

}
P
phpcmk author 12/11/2012



I don't see any increment by 1 in your code.

Why do you call the mysql_connect function? The database connection is active after the user login.
Check this solution:

$rs = CustomQuery("SELECT * FROM table_name");

while($row = db_fetch_array($rs))

{

if ($row["date1"] == "" || $row["date1"] == NULL)

{

$date1 = time();

$date2 = strtotime($row["date2"]);

$date3 = $date1-$date2;

$row["difference"]= floor($date3/(60*60*24));

//update here the difference field in the database

}

}



Hi,
I tried omit the mysql_connect and mysql_select_db and it gives me error.
Just wondering, the code used in phprunner is it different as stated below?

  1. "mysql_query" is replaced by "CustomQuery"?
  2. "mysql_fetch_array" is replaced by "db_fetch_array"?

C
cgphp 12/11/2012

Are you trying to connect to a different database than the one phprunner is connected to?