Jump to content

php/mysql math


quasiman

Recommended Posts

Hello,

I've found this script online that I'm trying to modify for my needs...still newbie at it though.
Basically it's supposed to take two values from the db table (AdmitDate and DischDate) and calculate the difference in days. If the dates are the same, make the value 1 day.

The error I'm getting is [code]Parse error: syntax error, unexpected T_ELSE in filename.php on line 13[/code]

[code]
<?php

// calculate difference and convert to days the absolute value; 1 day = 86400 s

$diff = abs($AdmitDate-$DischDate);
$diff = $diff/86400;

// remove decimals


    If ($AdmitDate == $dischDate);
        $diff = ceil($diff) + 1;
    else
       $diff = ceil($diff);

// Update records for LOS column in the database
$sql = "update PatientInfo set LOS=$diff";

// execute the select query
$open = execute_db($sql, $conn);

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/6305-phpmysql-math/
Share on other sites

might be worth checking the posting rules/etc as their is a forum for third party scripts (ie, stuff you never wrote yourself).

but hey, it's friday and i'm in a good mood so here you are:

[code]
If ($AdmitDate == $dischDate);
[/code]

the capital 'I' is not a problem, but best that it's all lower case (won't cause errors as far as i know, but i reckon it just looks rubbish). also - see the semi-colon at the end of that line? yup. you don't need it.

[code]
if ($AdmitDate == $dischDate)
[/code]

hope that helps :)
Link to comment
https://forums.phpfreaks.com/topic/6305-phpmysql-math/#findComment-22782
Share on other sites

The Dischdate field is a date, so when I create a record and leave it blank, it has an automatic value of 0000-00-00. I want the LOS (length of stay) field to account for this and never be less than 1:

[code]if ($DischDate == 0000-00-00){
    mysql_query ("UPDATE mytable SET LOS = (TO_DAYS(AdmitDate) + 1)");
}
else {
    mysql_query ("UPDATE mytable SET LOS = (TO_DAYS(AdmitDate) - TO_DAYS(DischDate) + 1)");
}[/code]

This seems to work, but it creates records in the LOS as 732738 when there's only a 4 day difference. I'm assuming that's a date converted to a number? I have the LOS field set to VARCHAR.
Link to comment
https://forums.phpfreaks.com/topic/6305-phpmysql-math/#findComment-23603
Share on other sites

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.