How to insert an irregular calendar events in mysql using PHP - php

I'm trying to enter events that occur over the same interval (Weekly, Bi-Monthly, Quarterly, Yearly). The script I'm showing is that of monthly..(If I can get that right, I can make an adaptation for the other intervals).
Events are entered along with the correct Week Number in which they occur.. But the following problem occur:
--- How can I make sure that months that has less than 31 days do not have events entered for them ?
----- If an event occurs on every 1st and 3rd friday (or whichever one), or last thursday of the month. How can I tweak the script below do do that.
Thank you.
A Sample of what I'm getting from mysql, when I insert...
+----+------+------+------------------+-----------+------------+----------+
| day| month| year | eventname | eventtime | eventplace | eventweek|
+----+------+------+------------------+-----------+------------+-----------
| 1 | 2 | 2011 | Social Gathering | 12PM | Room | 05 |
| 8 | 2 | 2011 | Social Gathering | 12PM | Room | 06 |
| 15| 2 | 2011 | Social Gathering | 12PM | Room | 07 |
| 22| 2 | 2011 | Social Gathering | 12PM | Room | 08 |
| 29| 2 | 2011 | Social Gathering | 12PM | Room | 09 |
+----+------+------+------------------+-----------+------------+----------+
PHP Code
<?php
if(isset($_POST['myform'])){
$day = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31');
$month = array('1','2','3','4','5','6','7','8','9','10','11','12');
$year = array('2011', '2012','2013', '2014', '2015', '2016');
$startday = mysql_real_escape_string($_POST['day']);
$eventplace = mysql_real_escape_string($_POST['eventplace']);
$eventname = mysql_real_escape_string($_POST['eventname']);
$eventtime = mysql_real_escape_string($_POST['eventtime']);
for($i=0; $i<count($year); $i++){
for($j=0; $j<count($month); $j++){
for($k=$startday; $k<count($day); $k = $k + 7){
$date = mktime(0,0,0,$month[$j],$k,$year[$i]);
$week = date('W', $date) ;
$query = mysql_query(" INSERT INTO caldemo(day, month, year, eventname, eventtime, eventplace, eventweek)
VALUES ('".intval($k)."', '".intval($month[$j])."', '".intval($year[$i])."', '".$eventname."', '".$eventtime."', '".$eventplace."', '".intval($week)."' )")or die(mysql_error()) ;
}
}
}
}
?>

Simple php solution:
function setDatePeriod($months=1, $date='')
{
if(!$date) $date = date('Y-m-d');
$d = new DateTime( $date );
$d->modify( "+{$months} months" );
return $d->format('Y-m-d');
}
echo setDatePeriod(3);

Related

php first week number every month

Don,t think I can find this answer in this forum.
How to get the first week number in every month where month start by Monday. This month first week is 36 how to get that? Having this code. But don't work.
//get first week number in month
$month = 9;
$year = 2018;
$day = 1;
$firstday = new DateTime("$year-$month-1");
$dow = (int)$firstday->format('w');
$firstday->add(new DateInterval('P' . ((8 - $dow) % 7) . 'D'));
$weeknumber = $firstday->format('W');
echo $weeknumber ;
I think this code will do what you want. It first creates a DateTime object for the first of the month, then it moves that date forward to make it a Monday. Finally it prints the week of the year using format('W').
Edit
Updated code to print first Monday and week number for whole year
$year = 2018;
echo "Month | First Monday | Week\n";
for ($month = 1; $month <= 12; $month++) {
$firstday = DateTime::createFromFormat('Y-n-j', "$year-$month-1");
$dow = (int)$firstday->format('w');
// update to a monday (day 1)
$firstday->add(new DateInterval('P' . ((8 - $dow) % 7) . 'D'));
echo sprintf("%5d | %s | %4d\n", $month, $firstday->format('Y-m-d'), $firstday->format('W'));
}
Output:
Month | First Monday | Week
1 | 2018-01-01 | 1
2 | 2018-02-05 | 6
3 | 2018-03-05 | 10
4 | 2018-04-02 | 14
5 | 2018-05-07 | 19
6 | 2018-06-04 | 23
7 | 2018-07-02 | 27
8 | 2018-08-06 | 32
9 | 2018-09-03 | 36
10 | 2018-10-01 | 40
11 | 2018-11-05 | 45
12 | 2018-12-03 | 49

calculating time attendance in PHP MySQL

I am working with laravel and i have a table with all the users attendances.
each row has a flag stating if the user logs in or out. I want to calculate the total working hours of user.
this is my table and sample data
id | user_id | date | timestamp | status |
1 | 1 | 2018-05-10 | 17:15:31 | out |
1 | 1 | 2018-05-10 | 13:15:31 | in |
1 | 1 | 2018-05-10 | 12:15:31 | out |
1 | 1 | 2018-05-10 | 08:01:31 | in |
I want to calculate the total working hours
$logs = DB::table('attendances as at')
->join('infos as in','in.user_id','=','at.user_id')
->select('in.name','in.avatar','at.*')
->orderBy('id','desc')
->where('at.user_id',$id)
->get();
$total_hours = [];
for($i=0; $i < count($logs)-1; $i++ ){
if($logs[$i]->status == 'out'){
$dattime1 = new DateTime($logs[$i]->dt.' '. $logs[$i]->time);
$dattime2 = new DateTime($logs[$i+1]->dt.' '. $logs[$i+1]->time);
$total_hours[] = $dattime1 ->diff($dattime2);
}
}
$working_hours = array_sum($total_hours);
Is this the best way to achieve accurate results? Please help.
Thanks
Can you try like this?
$time1 = "17:15:00";
$time2 = "00:30:00";
$strtotime1 = strtotime($time1);
$strtotime2 = strtotime($time2);
$o = ($strtotime1) + ($strtotime2);
echo $time = date("h:i:s A T",$o);
Output will be like this:
05:45:00 PM UTC

Change content of table according to week number

I am working on a script for a drivers license website, and I need to make a calendar like table for the students, so they can see when they have which lesson.
Right now we are updating the table manually, but I would like to make a script so it can do it automatically.
The table looks like this: (By the way, its a HTML table).
+----------+--------+---------+-----------+----------+--------+
| Week Nr. | Monday | Tuesday | Wednesday | Thursday | Friday |
+----------+--------+---------+-----------+----------+--------+
| 17 | 14 | 1 | 16 | 2 | |
+----------+--------+---------+-----------+----------+--------+
| 18 | 4 | 1 | 6 | | |
+----------+--------+---------+-----------+----------+--------+
| 19 | 8 | 1 | 11 | | |
+----------+--------+---------+-----------+----------+--------+
| 20 | 14 | 1 | 16 | 2 | |
+----------+--------+---------+-----------+----------+--------+
Lets say its this week (Week 17), it has its own set of lessons for every day, except Friday, which is the same for every week. Then it is the week after, which has its own set of lessons, and then it is 3 weeks after, which again has its own set of lessons. Then the 4th week, it start all over, with the same set as week 17, because its a 3 week program, over and over again.
What i want to do is that it automatically updates the table, so it shows the current week number. Then let us say that it is next week now, the table should have automatically update it self to show the current week and its set of lesson numbers.
The numbers under the column "Week Nr." are the week numbers, and the numbers under the day names are the lesson numbers.
So next week it should look like this:
+----------+--------+---------+-----------+----------+--------+
| Week Nr. | Monday | Tuesday | Wednesday | Thursday | Friday |
+----------+--------+---------+-----------+----------+--------+
| 18 | 4 | 1 | 6 | | |
+----------+--------+---------+-----------+----------+--------+
| 19 | 8 | 1 | 11 | | |
+----------+--------+---------+-----------+----------+--------+
| 20 | 14 | 1 | 16 | 2 | |
+----------+--------+---------+-----------+----------+--------+
| 21 | 4 | 1 | 6 | | |
+----------+--------+---------+-----------+----------+--------+
Is there anybody who could give me a hint on how to do that with PHP. I have tried everything I knkw, but I just cant get it right.
This is not exactly what you want, but it could be a good starting point. Just modify it to print out the HTML tags.
//Set a counter for the lessons
$j = 0;
//Loop through the weeks of the year
for ($i = 1; $i <= 52; $i++) {
echo "Week: ".$i."<br>";
echo "This weeks lessons: " . $j."<br>";
//Incrase counter
$j++;
if ($j % 3 === 0) {
//Reset counter if need
echo "<hr>";
$j = 0;
}
}

why does this code loop twice [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am having an issue trying to pinpoint a problem I am having, the code below loops twice and gives me an output of Match11Match111Match11Match111 I am really not sure where the problem is but it should only loop once.
<?php
function generateTimeCard($conn, $employeeID){
// set current date
$date7 = date("m/d/y");
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date7);
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow;
if ($offset < 0) {
$offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset*86400;
// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400){
$date1 = date("m/d/y", $ts);
$date3 = date("l", $ts);
$$date3 = $date1;
$date2 = $date1;
$day[$i] = $date1;
$stmt = $conn->prepare('SELECT `employeeID`,`date`,`unitNumber`,`jobDescription`,`timeIn`,`timeOut`, `lunch`, TIMEDIFF(`timeOut`, `timeIn`)
AS `totalTime` FROM `timeRecords` WHERE `date`= :day AND `employeeID`= :employeeID ORDER BY date,id;');
$stmt->execute(array(':day'=>$day[$i], ':employeeID'=>$employeeID));
$dayOW = 1;
while($row = $stmt->fetch()) {
if ($row['lunch'] == "Yes"){
echo "Match";
} else{
echo "1";
}
$dayCurrent = $date3 . "Hours." . $dayOW;
$data[$dayCurrent] = $row['totalTime'];
$timeDay = $date3 . "." . $dayOW;
$unitNumber = $date3 . "UnitNumber." . $dayOW;
$description = $date3 . "Description." . $dayOW;
$data[$timeDay] = date("h:i A", strtotime($row['timeIn'])) . "/" . date("h:i A", strtotime($row['timeOut']));
$data[$unitNumber] = $row['unitNumber'];
$data[$description] = $row['jobDescription'];
$dayOW++;
}
$stmt = $conn->prepare('SELECT `employeeID`, SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(`timeOut`, `timeIn`))))
AS `totalDay` FROM `timeRecords` WHERE `date` = :day
AND `employeeID` = :employeeID GROUP BY `employeeID`;');
$stmt->execute(array(':day'=>$date1, ':employeeID'=>$employeeID));
$row = $stmt->fetch();
$totalDay = "Total" . $date3;
$data[$totalDay] = $row['totalDay'];
}
$data['Week']= $Sunday . " - " . $Saturday;
return $data;
}
?>
My database looks like this
+----+------------+----------+--------+----------------------------+-------------+------------+-----------+---------+-------+
| id | employeeID | date | timeIn | jobDescription | equipType | unitNumber | unitHours | timeOut | lunch |
+----+------------+----------+--------+----------------------------+-------------+------------+-----------+---------+-------+
| 1 | 1 | 01/20/13 | 6:00 | Worked in RockPort | Excavator | 01E | 7238 | 17:13 | Yes |
| 2 | 1 | 01/21/13 | 6:00 | Worked in Jefferson | Excavator | 01E | 7238 | 17:17 | |
| 3 | 1 | 01/22/13 | 6:00 | Worked in Jefferson | Excavator | 02E | 7238 | 17:30 | |
| 4 | 1 | 01/23/13 | 6:00 | Worked in Whispering Creek | Skid Loader | 32SL | 2338 | 18:30 | Yes |
| 5 | 1 | 01/24/13 | 8:00 | Worked in Hubbard | Scraper | 54C | 9638 | 11:30 | |
| 6 | 1 | 01/25/13 | 8:00 | Worked in Jefferson | Dozer | 4D | 941 | 19:30 | |
| 7 | 1 | 01/26/13 | 8:00 | Pushed Snow | Loader | 950H | 342 | 20:30 | |
+----+------------+----------+--------+----------------------------+-------------+------------+-----------+---------+-------+
By default, PDO fetches with index and column-name keys. So there are "duplicate" values. You want to change the fetch mode to FETCH_NUM (only index keys) ($stmt->fetch(PDO::FETCH_NUM)) so that values don't appear under two mappings. http://php.net/manual/en/pdostatement.fetch.php

Determine if the store is open?

In PHP and MySQL - how to determine if the Store is Open or Close (return true or false)?
Also how to get the next opening hours if the store is closed?
Example of Opening_Hours table:
+----+---------+----------+-----------+------------+---------+
| id | shop_id | week_day | open_hour | close_hour | enabled |
+----+---------+----------+-----------+------------+---------+
| 1 | 1 | 1 | 16:30:00 | 23:30:00 | 1 |
| 2 | 1 | 2 | 16:30:00 | 23:30:00 | 1 |
| 3 | 1 | 3 | 16:30:00 | 23:30:00 | 0 |
| 4 | 1 | 4 | 16:30:00 | 23:30:00 | 1 |
| 5 | 1 | 5 | 10:00:00 | 13:00:00 | 1 |
| 6 | 1 | 5 | 17:15:00 | 00:30:00 | 1 |
| 7 | 1 | 6 | 17:15:00 | 01:30:00 | 1 |
| 8 | 1 | 7 | 16:30:00 | 23:30:00 | 0 |
+----+---------+----------+-----------+------------+---------+
The open_hour and close_hour are TIME type fields. Table design ok?
Example of current times:
Current time: Tue 23:00, - Output: Open, 'Open at Tue 16:30 - 23:30'
Current time: Tue 23:40, - Output: Close, 'Open at Thur 16:30 - 23:30'
Open on Thursday because Opening_Hours.week_day = 3 is disabled
Now how to handle the midnight time? This get more complicated.
As you can see, on Saturday (Opening_Hours.week_day = 5), it is open from 17:15 PM to 01:30 (closed next day Sunday)
If the current time is Sunday 01:15 AM, then the store would still be open base on Opening_Hours.week_day = 5.
Output: Open, 'Open at Sat 17:15 - 01:30'
In the past, I've handled this by using a time stamp without a date (seconds since midnight). So for Saturday, the open would be 62100 and the close would be 91800.
My thought was this removes some of the logic needed when a close crosses midnight, as you only need to compare the seconds since the start of the date to the time range.
And it's pretty easy to check if it's still open from 'yesterday' - just add 86400 to the current 'time' (seconds since the start of the day) and check against the previous day.
Probably all a single SQL statement.
You can use the PHP date() function and compare it to your opening hours.
You can do something like this recursive function (not working PHP code, but PHP combined with pseudo-code):
/* $current_time should be in the format of date("His") */
function check_hours($current_day, $current_time)
{
Get the MySQL row for today here
if (Opening_Hours.enabled == 1 WHERE Opening_Hours.week_day == $current_day)
{
if ((date("His") >= Opening_Hours.open_hour) and ($current_time <= Opening_Hours.close_hour))
{
// convert_numeric_day_to_full_representation isn't a real function! make one
return 'Open: ' . convert_numeric_day_to_full_representation($current_day) . ' ' . Opening_Hours.open_hour . ' – ' . Opening_Hours.close_hour;
}
elseif (date("His") < Opening_Hours.open_hour)
{
return 'Closed: Next opening hours: ' . convert_numeric_day_to_full_representation($current_day) . ' ' . Opening_Hours.open_hour . ' – ' . Opening_Hours.close_hour;
}
else
{
return check_hours($tomorrow, '000000');
}
}
else
{
return check_hours($tomorrow, '000000');
}
}

Categories