I've been given the task of fixing up a bit of very messy code one of our clients had written by a dodgy freelancer.... I'm not terribly keen on rewriting the whole thing, as its been in use for some time, its only really caused problems now when they've asked for another feature. The database is utilised by various other parts of the program and this function is only a small portion of the overall system.
Essentially the purpose of the script is to manage all meetings in a virtual office.
They need a page to display "current" meetings. This was there original query.
$CURRENT_TIME=date("Gi");
mysql_connect(localhost,$USERNAME,$PASSWORD);
#mysql_select_db($DATABASE) or die( "Unable to select database");
$query="SELECT * FROM ROOM_1 WHERE
EVENT_ROOM LIKE'%$URLROOMNAME%'
AND EVENT_YEAR='$CURRENT_YEAR'
AND EVENT_MONTH='$CURRENT_MONTH'
AND EVENT_DATE='$CURRENT_DATE'
AND START_TIME<='$CURRENT_TIME'
AND END_TIME>='$CURRENT_TIME' ";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
The time is stored in the mysql table in an int field as a 24hour value. Eg 3:43pm is stored simply as 1543.
The new requirement was for a meeting to be able to have a setup time allowance.
The freelancers... "ingenious" solutions was to add another int field to the table and change this line
AND START_TIME<='$CURRENT_TIME'
to..
AND (START_TIME-APPEAR_START_TIME)<='$CURRENT_TIME'
Now whilst it may work for some meetings it wont for others, eg A meeting starting at 1405 with a 20 minute setup allowance time would result in 1385...
So I'm looking for a clever solution that allows me to leave the rest alone and just subtract APPEAR_START_TIME field from the $START_TIME column in the query but by minutes.
Any ideas ?
If I understand you correctly, you have two integer values in the form of "HHMM", where HH is hours, and MM is minutes; and you want to calculate difference between two time values.
You can get HH with value / 100, and MM with value % 100. Then you can calculate delta for hours and minutes separately.
Hours delta is (HH values delta + probably 1 hour caused by minutes delta, if MM-start < MM-appear-start):
(START_TIME / 100 - APPEAR_START_TIME / 100) + (START_TIME % 100 - APPEAR_START_TIME % 100) / 60
Minutes delta is:
(START_TIME % 100 - APPEAR_START_TIME % 100) % 60
Then you can concatenate HH and MM parts of delta:
HOURS_DELTA * 100 + MINUTES_DELTA
first of all , thanks Kel :)
I think this one is more easier :
(((START_TIME/100)*60+(START_TIME%100) - APPEAR_START_TIME) / 60) * 100 + (((START_TIME/100)*60+(START_TIME%100) - APPEAR_START_TIME)%60)
Related
I'm trying to calculate a sort or "daily random number" in a range, wich can't be guessed, for each user in our site but can't figure out how to do it.
I don't want a random number either, it must be a calculated number in PHP, not an additional database field or anything similar.
I tought at a function who can take the user's ID and the day of year and calculate this number.
Example:
USERID: 12345, Range: 0-7 (constant values for every user)
DayOfYear: 250 (change every day)
Then something like: ((12345 + 250) MODULO 8) (so I've range from 0 to 7 for each user). The problem is that the same number will come out every 8 days in a loop that user will find very fast.
Each user don't necessarly need a different number for every day, even the same number would be OK for a few days but not all users must have the same number. Also, most important, no loop scenario, so user can't guess his his daily number.
Thank you for your help.
There are so many answers to this question and none would be the best ... but I'm in a funny mood:
$id = hexdec(substr(md5($userId . date('z')), 0, 3)) % 8;
Use the md5-function to get a hex-string from a string. Then use a part of this string to calculate the mod 8.
For the next 10 days the id will be 5,7,5,3,6,0,2,0,2,4 when using your user id
But to guess a number between 0 and 7 isn't so hard, don't uses this for security ...
I used this link to create a ranking algorithm for my website. It is working great. I basically have it done like this:
LOG10((s.views) + (s.likes * 2) + 1) * 287015 + UNIX_TIMESTAMP(m.check_time) AS Hotness
The query is then sorted by Hotness which gives me the largest rank on top. I would like to take Hotness which is a manipulated time value and convert it to a score such as 1 - 100. Hotness looks like this in the query: '1469612365.0402453'. Is there anything I can do with that?
I have found an excel file online that helps with the calculations of a Drugs Half-life and helps to determine how much of the given drug is likely to remain in ones system based on
Hal-life Hour Number
The quantity of the Drug taken per dose
how much is in your system from previous doses
Below is a screenshot of the Excel file showing both the Output with the calculations already performed and also shows the actual Math that is involved for each day...
The Columns A, B, C, D, E, etc.. is the Day 24 hours
Column D Row 6 is the Half life for a Drug in Hours
From the Image below you can see that the calculation is perfromed and that Value is then used in the Next Day's equation
Ok so I am not that knowledgeable with Math outside of basic Addition, Subtraction, Multiplication, and Division I do not know much more then that.
My goal is to create a tool similar to this Excel file but with PHP, I am not sure how to do so but I think all the answers are right here in the image above as far as the math portion.
Looking at D3 I can see that it takes...
D2's value which is 30 in the Image
It then Adds C3's value
Then Multiplies that by 1/2
I am not sure what the ^ does though?
Then it Divides 24 hours by D6 which holds the Hour Number
In my PHP I would like to have a Function that I can pass an array of Data, so let's say I pass in an Array with...
the Number of Days to calculate (my image shows like 4 days so if I pass 10 days, it will shows the daily results up to 10 days)
Then an array of the Daily amount consumed in mg (so in my image this would be an array with 30,0,30,0,0,0,0
Then I would also pass in the Half Life in Hours, so in my image the drug used has a half-life of 4.5 hours
This function would then return an array with the data for each day, should show the remaining mg in one's system for each day, I can then use this data result to build charts, graphs, or simply a List
I would appreciate any help to get me started, I think I can pull this off on my own but I need help getting the math portion 100%, above I break down the equation as I see it, please help me understand better for example I am not sure what ^ does in the equation or how to do it in PHP
I hope my question is not too vague, I will come back with more specific once I get a good start on this but please help if you can so far, thank you for reading.
function calcHalfLife( $mgTaken , $drugHalfLifeHours , $day ) {
//total number of half-lifes elapsed
$total_half_lifes = ($day * 24) / $drugHalfLifeHours;
//total reduction in dosage
$reductionFactor = pow( 0.5 , $total_half_lifes );
//return the current dosage in the person's system
return round( $mgTaken * $reductionFactor , 2 );
}
The above function should do the trick. Pass in the MG dosage taken, the number of hours for that drug's half-life, and the number of days since the dosage.
The function calculates the number of half-lifes experienced by the drug by taking the number of days * 24 hours in a day and dividing that by the total number of hours it takes for a half-life of that drug. This is the number of times the drug's dosage would be cut in half.
It then takes 0.5 (50% as a decimal) and increments it to the power of the total number of half lifes experienced by the drug. So 1 half life would be 0.5, 2 would be 0.5 * 0.5 = 0.25 etc etc. This is the decimal representation of the percentage of the drug left in the person's system.
It then multiplies that remainder by the original amount taken and rounds it off to 2 decimal places. The return floating point value will be a representation of the remaining MG dosage of the drug in the person's system.
If you're looking to build a function/system that allows you to calculate a daily dosage of that drug (ie: the person takes the same dosage each day as opposed to once) that is a very different formula and function, but the basic principle is the same as the one I wrote for you here.
Good luck ;)
I'm usually a lot better than this, I promise.
Back story here: At my job, if you are late/absent, you get attendance points. One way to work attendance points off is to work weekend hours. For every 12 weekend hours you work, you get 2 attendance points removed.
For example, if an employee has 26 weekend hours built up, I need to subtract 24 hours, leaving a 2 hour remainder, and remove 4 points.
Right now I have all of this in Excel, but I could probably just as easily create a quick php/mysql, but that's be a pain. What's my best approach here? I was think mod functions, but... yeah. My head hurts. Someone point me in the right direction?
Perhaps:
attendance_points -= (hours / 12) * 2 This assumes integer arithmetic.
then
hours = hours % 12
Does that help?
Are you allowed to use 6 hours in a chunk?
Basic update for each employee would look something like:
attendance_points -= weekend_hours / 12 * 2
weekend_hours %= 12
where / is integer division.
I run a digg-like website that promotes content to the front page when it reaches a certain number of votes. Right now it doesn't take date submitted into consideration.
I'd like to use a simple algorithm that just uses the number of votes and the date submitted to determine whether something should be promoted. I don't want the algorithm to do anything more complex then that (such as iterating over all the vote dates).
EDIT:
Shouldn't the formula be something like this:
30 / (days between post date and now) * (vote count) = weighted vote
Here are some scenarios which seem reasonable for my site, which indicates that the algorithm needs to be more lenient for older items (since older items are less discoverable on the site)
30 / 30 * 30 = 30 (30 days old, promoted with 30 votes)
30 / 5 * 15 = 90 (5 days old, promoted with 15 votes)
30 / 1 * 10 = 300 (1 day old, promoted with 10 votes)
How can the formula be modified so the above 3 give close to the same min weighted vote required for promotion?
You can use the difference between the current date and the submission date to weight the votes.
(threshold - (days between post date and now))/threshold * (vote count) = weighted vote
in code
$weightedVote = ($threshold - $daysOld) / $threshold * $voteCount;
This would have the effect of eliminating posts older than the threshold from consideration. For example, a post 10 days old would have its votes multiplied by 20/30.
Is there a reason why you are assigning an arbitrary number to content when the condition is vote based? I mean - it seems you'd be better off weighing the users and their votes rather than giving a piece of content more or less votes based on the date.
I wrote some pretty mean voting software for a company that had $10,000 + contests and our algorithm considered the user and their history of behavior, which ended up filtering out a lot of spam votes.
This sounds complex but it is not really.
As for your balancing code -
You want 1 day old content to be promoted at 10 votes, where a 30 day item requires 30 votes?
Or do you mean 1 day content with 10 votes is promoted, while a 30 day item with, say, 6 votes could be promoted because it is older and less likely to be seen, so the vote tolerance is reduced?
function daysDifference($endDate, $beginDate)
{
$date_parts1=explode("-", $beginDate);
$date_parts2=explode("-", $endDate);
$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;
}
$diff = 30 - daysDifference(date("Y-m-d"), $postdate);
if($diff > 0)
$weight = 30 / $diff + $votes;
else
$weight = $votes;
So, suppose the daysDifference function returned 26 and there were 4 votes originally. This would read 30 / (30 - 26 = 4) = 7.4 + 4 So 11.4 votes total.
For a one day old item with 10 votes, it would read 30 / (30 - 1 = 29) = 1.03 + 10. So 11.03 total.
Roughly the same for this sample, but will vary for others.
The if means that any content over 30 days is just not considered and their votes are equal to actual value.
I could have just misunderstood your needs though.