quasiman Posted March 31, 2006 Share Posted March 31, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/6305-phpmysql-math/ Share on other sites More sharing options...
redbullmarky Posted March 31, 2006 Share Posted March 31, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/6305-phpmysql-math/#findComment-22782 Share on other sites More sharing options...
quasiman Posted March 31, 2006 Author Share Posted March 31, 2006 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... Quote Link to comment https://forums.phpfreaks.com/topic/6305-phpmysql-math/#findComment-22786 Share on other sites More sharing options...
AndyB Posted April 1, 2006 Share Posted April 1, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/6305-phpmysql-math/#findComment-22787 Share on other sites More sharing options...
quasiman Posted April 1, 2006 Author Share Posted April 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/6305-phpmysql-math/#findComment-22932 Share on other sites More sharing options...
Barand Posted April 1, 2006 Share Posted April 1, 2006 [code]mysql_query ("UPDATE mytable SET diff = (TO_DAYS(AdmitDate) - TO_DAYS(DischDate) + 1)");[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6305-phpmysql-math/#findComment-23009 Share on other sites More sharing options...
quasiman Posted April 3, 2006 Author Share Posted April 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/6305-phpmysql-math/#findComment-23603 Share on other sites More sharing options...
Barand Posted April 3, 2006 Share Posted April 3, 2006 TO_DAYS converts a DATE type field to the number of days since 1970-01-01 Quote Link to comment https://forums.phpfreaks.com/topic/6305-phpmysql-math/#findComment-23656 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.