PHP/MySQL time date variable calculations - php

I have 2 variables, one a date like 06/02/2018 and a day interval that changes. I need to add the two together, then find the difference from the current date.
I've read carbon can help with this but I'm stuck.
say the vending machine was last filled on 6/20/2018 and it needs to be refilled every 30 days. I need to find out how many days from today it needs to be serviced.
Please help

Related

Extract Working/Non-Working Hours With Weekend/Weekday Breakdown For A Working Schedule

I do need a calculation script for my project which would be calculating 4 things in minutes for me.
Total assigned minutes in working hours for weekdays (Between 08.30 - 17.30)
Total assigned minutes in out of working hours for weekdays (Except 08.30 - 17.30)
Total assigned minutes in working hours for weekends (Between 08.30 - 17.30)
Total assigned minutes in out of working hours for weekends (Except 08.30 - 17.30)
Basically, I am creating and using a schedule Google Calendar actually but doesnt matter, I just have start-end datetime objects in my hand for the employees on a calendar and assigning datetime ranges to employees to make them responsible for answering the customer calls in a certain time range which also could last a week, a few hours, a few minutes or a few days. The thing here is those date ranges are pretty flexible.
I've tried looping over the unix timestamp, creating a DateTime object per loop and check those 4 things but that would have been too much memory&cpu usage as I locked my computer a few times. I would be able to loop over hours in day if the events could only last a day at maximum but they are very flexible so I need a strong algorithm here.
For example a schedule would look like below:
Start(DateTime Object) => 2022-01-27 00:00:00
End(DateTime Object) => 2022-01-29 13:30:00
The function should take those two objects as an argument and should create an output like in the picture I ve shared below. Should be similiar to this:
function createReport(DateTime $employeeWorkStart, DateTime $employeeWorkEnd) : array {
...
return [
'weekday_in-working-hours' => XXX,
'weekday_out-working-hours' => XXX,
'weekend_in-working-hours' => XXX,
'weekday_out-working-hours' => XXX,
]
}
So I need to create a monthly-basis report which shows how many minutes I've assigned for each employee in the schedule.
My working hours are between 08.30 - 17.30, saturday & sunday is considered as weekend.
Example Report Output
So, not able to provide an actual code-answer right now. But how I would approach it is the following:
Get the amount of days between the two dates, subtract weekends. Also subtract one day if the $employeeWorkEnd is the current day (today) and if you want to have extra precision. I found this gist that gives you the working days (weekends and holidays excluded): https://gist.github.com/quawn/8503445
Multiply this by 9 (working hours) and then by 60 to get the total minutes. This is the total time worked.
If you wanted the extra precision in step 1, now just take the difference in minutes between 09:30 and the current time (date_diff can provide this). Add this difference to the total you had so far.
Execute this procedure for every employee you have. The above procedure will not do any loops, I believe it should be possible to do it just by subtraction and multiplication (if you want to exclude holidays using the code in the gist, this will introduce a small loop assuming low amount of holidays).
Your example output shows hours but your description shows minutes. The above story will give you minutes but it's just as easy to get the hours (or milliseconds for that matter).

Retrieve remaining time between two dates considering business hours

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."

What is the best practice in creating scheduling calendar in php and html?

I want to create a website with an scheduling calendar.
My first idea is to use some free calendar template or download some free scheduling calendar. Then in my scheduling form, when someone request for an schedule, I will get the date he/she input and save it into the database then show it to the scheduling calendar.
But someone told me that, in my database, I should create a calendar table.
Which is the best way around?
The first one with only one table for schedule on my database or the second one with two tables for schedule and calendar?
I hope you get my idea.
It could be first one. One of option is to keep data by day of year.
you can draw your own calendar by counting day of year
actual day of yaer - date('z') + 1; //+ 1 because it is an array it starts from 0
then you can get number of days in each month
cal_days_in_month
and loop it x 12 with
here will be day of month with css style so it looks like calendar field
your i++ will bee number of days in month of course.
Keep records in database by year and day of year. you can do so much things this way

Best way to determine and store Pay Periods for a time clock in PHP and MySQL

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

Schedulings pages after 24 hours based on registration date.

I have a online course having 50 lessons.
Now what i want to achieve is that each lesson is shown 1 day at a time or every 24 hours depending on the date the members are subscribed?
Note:- members Can subscribed at different dates and times.
Any idea how can i achive that?
I am not sure this is a scheduled task, but more of a bit of logic in your php code that doesn't show the link for courses that are not accessible yet.
This should be fairly simple date processing based on a users registration date.
Here is a post on stack overflow for calculating date differences.
PHP date calculation

Categories