It's hard to articulate the problem I'm having in the title, but here's the scenario: I'm working the backend of a 3-tiered exam taking website. I'm currently updating my function used for when students request an exam. The exam has start and end dates and times, as well as a time limit. What I want to do is specific to the case when the amount of time left in the exam period is less than the time limit for the exam, For example, the exam period ends at 5:00PM. The time is now 4:30PM. The exam time limit is an hour and a half. I want to replace the hour and a half time limit with 30 minutes. To do that, there are 3 dates I need, and I will explain how I get each:
Timestamp for my function page. When the controller calls my page, after I open the session, I set the default timezone to America/New York (my region). To get the timestamp, I use the code:
$date = date('Y-m-d H:i:s');
$currenttime = date('H:i:s', strtotime($date));
I use a function call to my database to acquire the time limit and the time the exam ends (I also use the dates for other comparisons to check exam availability). The exam end time is stored in resultant array $res[10] and the time limit is in $res[11].
Assuming the due date for the exam is equal to the date of the timestamp, and it is in the exam period (2 checks I perform before), I want to check if the difference between the current time and the end of the exam is less than the time limit. If the time until the exam ends is less than the time limit, I want to set that amount of time as the new time limit. My front end expects my result in the form HH:mm:ss (php's equivalent is H:i:s). So if there is 30 minutes until the exam ends and the time limit is an hour and a half, I want the new time limit to be 30 minutes (or 00:30:00). My code is currently not working as desired. I am meeting the criteria for the if statement, which is my desired result. When I am converting it to the form H:i:s is where the problem seems to be (I keep getting a result around 19:00:00). Every article I've read suggests to do it this way, but I may be missing something.
$compareTime = strtotime($res[10]) - strtotime($currenttime);
if(compareTime < strtotime($res[11]){
$timelimit = date('H:i:s', $compareTime);
}else{
$timelimit = $res[11];
}
If there is a better way of doing this, I'd be much obliged to know. I'm fairly new to php, and admittedly I realize this may be a strange problem, but that's the way the group decided to go.
Maybe it's because the databse and PHP server are using different timezones. Check it out by printing these variables to see their values.
Related
I am trying to write a php solution to calculate the planned end time considering the target in business hours.
It shouldn't consider some days (retrieved from setting saved in db) such as holidays.
Also business hours are retrieved from db (morning_from (8:30am), morning_to (1:00pm), evening_from (2:30pm), evening_to (6:30pm)).
I want to develop this script because I want that my page shows the remaining time for technical resolution of an opened ticket every day.
For example:
customer having contract with 10 working hours SLA opens a ticket
today (friday) 31/01/2020 16:00:00, considering that in the
noBusinessDays = array("saturday", "sunday") and businessHours set as mentioned before(8:30-13:00/14:30-18:30), the result will have to
be monday 3/02/2020 17:30:00.
Code example:
$noBusinessDays = array("saturday", "sunday");
$businessHours = array("morning_from" => "8:30", "morning_to" => "13:00", "evening_from" => "14:30", "evening_to" => "18:30");
$SLA = "10"; //hours
$ticketDate = new DateTime();
$ticketDate->setTimestamp(strtotime("31/01/2020 16:00:00"));
// I don't know how to use my arrays to say in this calculation how to use them
$maximumLimit = $ticketDate->add(new DateInterval("PT" . $SLA ."H"));
Thank you in advance.
You may use the following function
// intersection of 2 time intervals
// input - Unix timestamps (start,end)
// output - intersection in seconds
function time_union($b_1,$e_1,$b_2,$e_2)
{
return max(0,$e_1-$b_1 - max(0,$e_1-$e_2) - max(0,$b_2-$b_1));
}
You will start with an empty time interval [X, Y) where X is the timestamp of the ticket creation and Y initially is equal to X.
Then you start adding days to Y - one by one. Each time you expand the time interval to contain another day - you use the above function to check how much of the SLA hours are covered (i.e. overlapping) with the working hours in the day you have just added. If the day is marked as a holiday - you simple skip it and continue with the next date.
If you find out that SLA hours are partially covered with either the morning or evening business hours - you should simply subtract the extra hours.
In the end Y will be equal to the timestamp that you want to show in your application.
I think I'd break down the problem into pieces. After calculating the total number of days in the interval, first dispose of the trivial case that it's all happening in one week.
begin by calculating the number of "whole weeks." Each "whole week" is five business days. Subtract the corresponding interval of time and proceed. Now, look at the day-of-the-week of the start-date: each day adds a certain number of days. Then the day-of-week of the end date, likewise. You can then consider hour-of-the-day as needed.
Holidays are a simple table: if the day falls within the range, subtract one day.
Now ... having said all of that, the very first thing that I would do is to search GitHub and SourceForge! Because I am extremely sure that somebody out there has already done this. :-D
"Actum Ne Agas: Do Not Do A Thing Already Done."
I'm a beginner for php and developing this web application, which users who registered on this site, can be claimed some scores in every one hour. When a user claims at some time, database stores that time as time data type in to user_claim_time column. When that same user tries for his next claim, this php script be able to get his last claim time and add one hour to check if the user really claims in an one hour.
So, my question is how can we add one hour to queried time. I'm using php time(h:i:s) function to store server's current time into the database.
You can do something like this:
SELECT * FROM your_table
WHERE user_claim_time < NOW() - INTERVAL 1 HOUR
However i recommend you to use user_claim_time column in datetime format.
Because time like this '00:00:00' will produce negative output as one hour subtraction can change the date or month as well. For example date like this '2017-08-01 00:00:00'.
So using datetime is the right way i think to properly compare time difference.
I am creating a sort of ticketing system where cases are created and have a set time period to be answered in.
The start-time is stored in the database as a timestamp e.g. 2013-10-16 01:39:00, datetime field in mysql. There is also a resolve-by timestamp stored which could be set to 8 hours after the start-time.
When a case is either marked as resolved or put on hold a stop-time timestamp is stored in the database as well as a clock_stopped flag. This means that the 8 hour time countdown is stopped. When a case is no longer on hold or is re-opened then the time countdown is restarted. But its not to include the period of time which the clock was stopped. So the total time to resolve case remains 8 hours even if it was on hold for a day. Make sense?
What is the best way to work out elapsed time when including periods of time when the elapsed time is not counting down as above? Do I need to add in more fields to store in the database e.g. How would you do it?
I am building a Time Clock application with PHP and Laravel 4.
My boss requires that he is able to pull and build different reports based on the data I store in the database for a Time Card record.
Right now I store a DateTime for clock in and clock out as well as a Timestamp for both those times as well into the Database.
I need to be able to Query the database and build reports for different Pay Periods for a user.
So for example I will store in another Database Table, records that will be for a User ID and will have different Pay Periods. So a Start day may be the 1st of the month and end date the 15th and that is 1 pay period (roughly 2 weeks) I am not sure the best way to store these records really.
Another will be the 16th of the month to the end of the month. So the end date would be different depending on how many days are in a month
I am not sure about the best way to define these Pay periods for a user. I can't simply say 1-15 and then 16-30 since the 30 would be a different number for each month.
Would appreciate any insight into how this could be done?
So I can build reports for any Pay Periods since not every user gets paid every 2 weeks it needs to be flexible so that I can define it on a per user basis
This question is more about the Logic instead of actual code.
Welcome to the wonderful world of Time and Attendance. You are touching the tip of the iceberg. You may find that purchasing a pre-packaged product may be easier than writing your own.
That said, I can offer you the following general advice:
Be very careful of your data types and how they are used, both in PHP and in MySQL.
You need to make sure you understand local time vs UTC, time zones, and daylight saving time. In general, you don't want to store local time unless you also store its offset from UTC. Otherwise you will have ambiguity around daylight saving time changes. This is important even if you only have one time zone to deal with.
When it comes to Pay Periods, the common types are:
Weekly
Bi-Weekly
Semi-Monthly
Monthly
Every X days starting from Y
In some systems, each individual pay period can be adjusted +/- a number of days from it's normal date. When doing so, the bordering period must also be adjusted to compensate.
You should start with business logic that can calculate the start and end date for a pay period given any particular date and time. You can then expand that to easily get the prior or next pay period.
You can store each pay period into it's own table, but it's not necessarily required. That will depend on a lot of specifics about your system internals.
Because a pay period is defined by dates, you have the "Whose Day is it?" problem. It might be the day as defined by the company, or if employees are in different time zones, then it might be the "logical day". If you only have one time zone to deal with then, you are lucky in this regard.
When comparing against the pay period, use half-open intervals, [start, end). In other words:
periodStart <= punchTime < periodEnd
or likewise
periodStart <= punchTime && periodEnd > punchTime
The end of one period should be exactly the same as the start of the next. Don't try to define the end of the period at some silly value like 23:59:59.999...
As you can see, this is just the beginning. I hope this is useful to you. If you can narrow the focus of your question further, I'll be happy to help more. Otherwise, it's like asking for how to build an ERP system when you're not sure what structure to store inventory.
I think you are over thinking this. Let thte user define the start and end dates.
You will need the UserId, a timestamp (time in and time out) of the user and that should be about it.
I picture something like this:
UserId | DateIn | DateOut
On the page you could put put dropdowns (or if you want a nifty interface a datepicker that uses javascript) and allow the manager to pick a start and end date that he wants to choose.
So if he wants to see an employees time between Jan. 1 and Feb. 31 he can choose those as his start and end dates.
This will allow things to be very flexible, for example the manager can choose Feb 16 as start date and Feb 29 as end date. It makes sense to allow him to choose the data requirements so he can view whatever he wants.
EDIT:
An example from my comment below this post you could do something like:
$startDate = new DateTime();
$startDate->modify('first day of this month'); //or 16th for second part of bi-monthly
$startDate->format(#some date formatting as you need#);
$endDate = new DateTime();
$endDate->modify('last day of this month'); //or 15th for first part of bi-monthly
$endDate->format(#some date formatting as you need#);
If things are even less defined however you could always try doing special math. date('t') will give you the number of days in a month. I would refrain from using this unless your pay days are fixed such as paid every 6 days.
In general I would harness the power of the PHP DateTime class over using date() function. http://php.net/manual/en/class.datetime.php
I've got two times that need to be stored into a database. The time format is hh:mm:ss with NO DATE. These times can be changed by the users on the system. One is a time on and the other is a time off. The time off should always be greater than the time on within a 24 hour cycle.
Here is the part that I'm having trouble with. I don't want to limit the user to selecting times before midnight to keep everything in the same "daily" cycle so I'd like to be able to logically determine if the users' times are simply within a 24 hour time period and then test that the on time is always less.
Can someone help me work through this? There are so many time and date functions that I really don't know which one(s) I need to do this; plus, I'm unclear on how I should test for this.
I'm starting to think that there is no way to test for this without having a date included. Just the times is not enough.
The time is always within a 24 hour cycle, so if the user puts 01:00/03:00 he's on for 2 hours
If he writes 03:00/01:00 he's on for 22 hours.
I dont see the problem.
The OP wrote in a comment:
The user can opt to get a report
delivered in a window of time. The
user may opt to have their reports
delivered in a window from 23:00:00 to
01:00:00 hours. They may decide
tomorrow that that time is no longer
good and change it to 23:0:00 to
05:00:00 or something like that. Am I
missing something??
You have no problem in the time definition part. You may want to play with the code that sends out the report.
// current time
$timeNow = time();
// fetch user time options from database
$timeOn = [from the database];
$timeOff = [from the database];
// convert times to seconds from epoch
$timeOn = strtotime($timeOn);
$timeOff = strtotime($timeOff);
// if database time is in timestamp format,
// only the hour, minutes and second information is needed
$timeOn = mktime(date("H", $timeOn), date("i", $timeOn), date("s", $timeOn));
$timeOff = mktime(date("H", $timeOff), date("i", $timeOff), date("s", $timeOff));
// if time on is higher than time off, time on is of yesterday
if($timeOn > $timeOff){
$timeOn = strtotime("-24 hour", $timeOn);
}
// decide on report sending
if($timeNow >= $timeOn && $timeNow <= $timeOff){
// Send report
} else {
// Do not send report or reschedule the report
}
Any two times in hh:mm:ss format are going to be within a 24 hour time period, as you state. So unless you actually store a date, I am not sure how you can do this.
If I understand correctly, a start time of 23:00:00 and an end time of 04:00:00 should be acceptable to you (this just means 5 hour work shift)? If this is acceptable, then can you give me an example of unacceptable input?
Perhaps you want to check that the end time is within 12 hours of the start time? That should be feasible.