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.
Related
Hep hey!
I am building an statistic overview of how many people is supposed to be at work at any given 5 minuts interval on a given day.
Say, we have 6 people working at 10.50, same at 10.55, then one go home and we got 5 people working at 11.00
Now, the way i imagined to keep track of this was to have an array with 5x12x24 elements (1 element per 5 minuts for an 24 hour interval), where i run through each employees shift time and increment the elements for the given 5 min intervals their shift takes them over.
(say a person works from 9.00 to 10.00, then i will increment the values from 9.00, 9.05, 9.10 up to 10.00 by one)
I need the data to make a diagram later, that is why i store it in an array.
Now my question is, which way is the fastest to do this?
Should i start out with an array which contains all the time elements and then increment it as i run through the shift hours of the employees ($arr['9.05']++) or should i start out by making an empty array and just check if the value of the time exsists, if not, create that element and if it does, increment it?
Or is there in general a smarter way to do this?
I ask as i can see this becomming a pretty heavy operation if you have 50+ employees which have to run through this function, so the smarter it can be made, the better :)
PS. the shift times comes from a database that i do not have access to, so i only have the timestamps of the start of the shit and the finish.
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 ;)
Still working on my planner/calendar application. I'm nearly done, I got some of the harder parts working but I'm stuck at one more difficult part. I want to show my events in a grid according to their start time.
It doesn't show in this picture, but pretend there's a column with hours (8am - 11pm or so) at the left of the 25th. If an event starts at.. say, 1pm, I would like it to show somewhere in the middle of the page. If an event starts at 8:30 am, it should show between 8am and 9am.
I guess I could do this with tables, but I was wondering if there's another way. Is this doable with plain html/css, perhaps some Javascript? Any suggestions on what the best way would be to achieve this? If I use a table, I'm still not sure what would be the best way to do this. A cell for every thirty minutes? I have access to the start and end time of each event from my view. An event array (in this example, the 25th) looks like this:
Array
[1] => Array
(
[title] => Ethiek
[description] => Ethiek: Opdracht 1
[time_start] => 11:30:00
[time_end] => 12:00:00
)
[2] => Array
(
[title] => Project Management
[description] => Test: Project Management
[time_start] => 15:00:00
[time_end] => 16:00:00
)
[event_count] => 2
I appreciate any advice you can give me. Thanks a lot!
EDIT: Started a bounty on this because I'm still stuck and I would appreciate some feedback.
UPDATE:
I've been breaking my head over this and I honestly can't figure out the best way to do this. First of all, I think the reason I'm stuck is the way I read out my events from the db/array. This is the code I have to display the events as seen in my screenshot, don't mind my complex arrays:
foreach($details[0] as $key => $detail)
{
echo "<div class='grid'>";
$header = "<p class='detail_header'>";
$header .= ucfirst($dates[0][$key]['name']) . ", " . $key . " " . $init['curr_month_name'];
$header .= "<img src='" . base_url() . "assets/images/create_event.png' alt='Plan iets'></p>";
echo $header;
for($i = 1; $i <= $details[0][$key]['event_count']; $i++)
{
echo "<div class='event " . $details[0][$key][$i]['type'] . "'>";
echo "<p class='event_title'>" . $details[0][$key][$i]['title'] . "</p>";
echo $details[0][$key][$i]['description'];
echo "</div>";
}
echo "</div>";
}
It's a bit of a mess, not to mention that I have the same code another time to fix some exceptions. But more importantly.. I feel like those loops don't allow me to make a lot of modifications to it. I tried adding two divs for AM and PM so I could split up the events in before-noon and afternoon blocks, and then just display the time on the event (to avoid having to work with a thousand blocks of 15 minutes). But yeah.. That didn't work out since it would put a couple of 'PM' divs if there is more than one event in the afternoon.
I'm tempted to just leave it like it is for now and just display the start/end time in the event divs.. until I figure out a better way to read them from the array and display them.
Any help/suggestions appreciated. Thanks.
I'm actually also doing this right now. My solution was to go with 960.gs-like divs.
First, I define a series of constants: Start time to display, end time to display, columns per hour, total columns. In my app's case, these variables are configurable by the user.
Second, I query an array of events that I need to deal with. These include a start time and end time, plus the details I want to display. I'll be using jQuery QTip to popup details that hover, so data to populate those is also included in this query.
Now, the 960.gs concept. The basis for a grid is knowing that you have X amount of space to display your content...with 960, it's 960 pixels. Mine is more custom, but this provides the concept. You can divide this by quite a few numbers, which becomes the basis for how to split the grid. Using this approach, I can easily define a column from grid_1 to grid_4, and it will take a width that is a commensurate percentage of the overall width (i.e. on a 16 column layout doing a 4 column div would cover 25%) It's cross-browser compatible, and doesn't require an overt amount of clear divs. You just need to make the numbers add up to match the amount of columns you want to work with.
Now, I begin by doing the math to figure out how much time each column represents. I assemble each day using a foreach loop: I start with the hour of the display start time and increment up. If the start_time of an event equals the incrementer, I start a div that's styled appropriately based on my coloring criteria. Likewise, if my end time <= the incrementer, I stop the div and define the column's width in the id. Obviously, at the end of the loop, I do an incrementer++. Repeat per day that you display.
My concept is doing this on an time basis for a weekly type calendar. But the overall idea could easily be modified for a month-style calendar or even for a day calendar.
Tables definitely make this easier (version 1 was tables) but it can be done either way if you have the patience.
Your idea about tables sounds like the best option. Assuming that the table borders are all hidden, you can go down to whatever time slot size your application dictates without making it look too messy.
I'd just start with a row representing 15 minutes if they just need to line up roughly at the correct times. Your :hover effect on each block could show the exact times, and the table is then just there to allow you to rowspan your events through rough 15 minute timeslots to make it line up enough that it's visually representative.
You could go with a css approach and absolute positioning of your calendar entries. You would then need a css class for each timeslot. Just to give some orientation:
.timeslot_0 {
position: absolute;
top: 0px;
}
.timeslot_1 {
position: absolute;
top: 50px;
}
.timeslot_2 {
position: absolute;
top: 100px;
}
<div id="calendar">
<div class="day">
<div class="entry timeslot_0>Schedule Enty</div>
<div class="entry timeslot_2>Schedule Enty</div>
</day>
<div class="day">
<div class="entry timeslot_1>Schedule Enty</div>
</day>
<div class="day"></day>
<div class="day"></day>
<div class="day"></day>
</div>
It's the same idea as it is bedind 960grid
As an alternative you could maybe use a percentage for the position from the top so 8am would be 8% and 6pm 100%. It's a bit tricky depending on the available size and stuff but could work out. Haven't tried it yet. Just stuck my mind as another possibility.
I would advise not to use tables, but instead Javascript and CSS.
Use Javascript to get the width of the calendar background: eg: 800 pixels.
Split this width to accommodate 30 minute slots for 24 hours; 800 / (24 * 2) = 16 pixels
24 * 2 (48) is how many 30 minute slots there are in 24 hours.
16 pixels is how much space you have per 30 minutes.
For each event you have, take it's start time in blocks of 30 minutes and multiply it by 16 pixels to get it's position from the left hand side.
For an event that starts at 2:30 (5x 30 minute blocks), 5 * 16 = 80 pixels from the left
For an event that starts at 12:00 (24x 30 minute blocks), 24 * 16 = 384 pixels from the left
For an event that starts at 14:30 (29x 30 minute blocks), 29 * 16 = 464 pixels from the left
I suggest the easiest way to implement this is by using the power of jQuery;
var usableWidth = $('div.calendar').width();
eventsCollection.each(function(singleEvent) {
$(singleEvent).width = (getStartTimeInMinutes(singleEvent) / 30) * (24 * 2);
});
In order to do this, you would need to represent your events in JSON (Javascript Object Notation). Using php, this is easy with json_encode().
This method can be a little complicated to understand at first, but it keeps your markup (HTML) very clean and works really well with any size screen. Most importantly, as your events will be represented as Javascript Objects, it gives you a really great starting point to making a really rich Ajax application.
glad to hear you're making progress.
Tables still appear to me the best solution for this. Think about this:
- you can style width, height, etc, to set them in a grid like way, which is what you want.
- you know exactly the structure, so running through the table is simple loop code
And the clincher for me:
- tables provide accessibility. Because screenreaders have ways to interpret tables in meaningful ways for sight-impaired viewers, the tables a particularly helpful when the contents is made meaningful by the rows and columns, which is exactly what you've got.
By the way, put in the URL of a running version for us when you're done.
Yo. I have been looking at a similar problem for a calendar module of a website I have to build. I am putting it together in c# so the code would not really suite your answer full but the way that i've looked at data may help.
Now tables are pretty hard so forgive the crudness of this :P
-------------------------------------------------------------------------------------------
hrs | mins |total mins|
day start time | 10 | 0 |600 |running time in mins
day end time | 23 | 30 |1410 |810
item start time| 13 | 0 |780 |running time | distance to cover | starting point
item end time | 16 | 40 |1000 |220 | 27.16% | 180
-------------------------------------------------------------------------------------------
If you will look at the above. I have a day with start time or end time. This can be converted to what ever time format you need week month whatever. The point is that you can look at all your items and find the earliest start date and latest end time calculating 100% of your event distance(for me 810 mins).
From here you can calculate % points that an individual even needs to cover
(single event run time/total event runtime * 100). This would be the distance to draw that individual item.
And give that item a starting point(this should be a percentange as well) but starting point for event one is 180 mins(total event start time - item event start time )
Thanks.
KJ
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)
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.