Jump to content

Archived

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

quasiman

php/mysql math

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]

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites
AH HA!! Thank you!

Unfortunately it's not updating the days correctly, but at least I'm past the error!


Sorry it's in the wrong forum...

Share this post


Link to post
Share on other sites
[code]$sql = "update PatientInfo set LOS=$diff";[/code]
Assuming you only want to update one record, your query will need a WHERE clause as well ..

[code]$sql = "update PatientInfo set LOS= '$diff'  WHERE some_field = '$some_value' ";[/code]

Share this post


Link to post
Share on other sites
Hi AndyB,

Actually I want it to update all records. I have the script included on the index page, so on every load it updates based on the other two fields.

Share this post


Link to post
Share on other sites

[code]mysql_query ("UPDATE mytable SET diff = (TO_DAYS(AdmitDate) - TO_DAYS(DischDate) + 1)");[/code]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
TO_DAYS converts a DATE type field to the number of days since 1970-01-01

Share this post


Link to post
Share on other sites

×

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.