Increase days automatically in database - php

I have an application where each user can request their vacations. There are two types of users, fixed and temporary. Fixed users have 24 days a year for vacations. The doubt comes with the temporary users.
Temporary users are added 2 days per month worked until December 31 of that year. For example:
user1 entered the company on 25/01/2019, until 31/12/2019 would have to increase it 2 days per month worked until that date, in total 22 days. As of 01/01/2020, those 2 days, will be increased every calendar month and start from 0, so that 01/01/2020 would have 0 days and 01/02/2020 would have 2 days.
Is there a PHP function or in MySQL a procedure for those 2 days to automatically add them to the database?
In the database I have a user table that has the following fields:
name available_days start_date
======= ================= ============
user1 0 25/01/2019
To those available_days is to which the days must be increased.
I have this function that calculates the difference in months since the user entered the company until today:
function difcurrentmonth($startdate){
$date = new DateTime($startdate);
$currentdate= (new DateTime)->format('Y-m-d H:i');
$finaldate = new DateTime($currentdate);
$dif = $date->diff($finaldate );
$month = ( $dif->y * 12 ) + $dif->m;
return $month;
}
$month = difcurrentmonth("2019-01-25");
$available_days= $month * 2;

Not quite an answer; too long for a comment...
-- Today is the 21st February 2019.
-- A temporary employee started work on 15th August 2018.
-- Accrued holiday resets on December 31st.
SELECT GREATEST('2018-08-15','2019-01-01') range_start
, '2019-02-21' range_end
, DATEDIFF('2019-02-21',GREATEST('2018-08-15','2019-01-01')) duration
, ROUND(DATEDIFF('2019-02-21',GREATEST('2018-08-15','2019-01-01'))/30,0) accrued;
+-------------+------------+----------+---------+
| range_start | range_end | duration | accrued |
+-------------+------------+----------+---------+
| 2019-01-01 | 2019-02-21 | 51 | 2 |
+-------------+------------+----------+---------+
In your scenario, '2018-08-15' would be replaced by a column from your database.

Firstly you will need to write a query that will get the available_days from the User table for user1.
I will assume you are using PDO for this example.
$stmt = $pdo_conn->prepare("UPDATE user SET available_days = available_days + 2 where [name] = :userName");
$stmt->bindParam(':userName', "user1");
$stmt->execute();
Create a cron job that runs on a certain day of the month. You might want to add the last_modified_date column to use as a safe catch to prevent updating the user multiple times within a given month.

Related

How to filter day and date from database using PHP and MySQL

I need to fetch day and date from database using PHP and MySQL. My table is below:
db_day:
day_id day_name
1 Monday
2 Tuesday
3 Wednsday
4 Thursday
5 Friday
6 Saturday
7 Sunday
The above is my day table.
db_special:
id restname date_from date_to day_from day_to
1 aaa 2016-09-22 2016-09-26 1 4
2 bbb 2016-10-19 2016-10-28 2 5
3 ccc 2016-10-18 2016-10-25 4 7
The above are my two table. Suppose user has the input Tuesday and it need to be fetched the value within date range from the above table.Here if user has the input only Tuesday,first it should calculate the day and date. If suppose today's day is Friday it will consider next Tuesday onwards.
Here user input is only Tuesday so it will calculate today's date e.g-2016-10-21 and then will filter value from the above table. The expected result as per this example should be this row from the table:
bbb 2016-10-19 2016-10-28 2 5
How can I write the query to resolve this problem?
You can try with this code:
// $db is PDO instance
// $userDay is user input day
$todayDayId = date('N');
$stmt = $db->prepare('SELECT day_id FROM db_day WHERE day_name=:userDay');
$stmt->execute([':userDay' => $userDay]);
$userDayId = $stmt->fetchColumn();
if($todayDayId > $userDayId) {
$diff = (7-$todayDayId) + $userDayId;
}
else {
$diff = $userDayId - $todayDayId;
}
$wantedDate = date('Y-m-d', strtotime('+'.$diff.' day'));
$pdoData = [
':wantedDate' => $wantedDate,
':wantedDate2' => $wantedDate,
];
$stmt = $db->prepare('SELECT id, restname, date_from, date_to, day_from, day_to
FROM db_special
WHERE date_from>=:wantedDate AND date_to<=:wantedDate2');
$stmt->execute($pdoData);
$result = $stmt->fetchColumn();

MySQL query for wordpress day events

I'm a WordPress (PHP) developer, however I have not done a lot of complex MySQL queries. I am working on an events website and want to create filters: User should be able to filter events by the following criteria: TODAY, TOMORROW, THIS WEEKEND, NEXT 7 DAYS, CHOOSE YOUR DATES.
Meta data used to filter events is below and can be found in the post meta db table in the meta_key column.
start_date
end_date
times
recurring_event
days
sold_out
cancelled
This is how the table looks like:
id post_id meta_key meta_value
1 12 start_date 20140923
2 22 days a:4:{i:0;s:6:"monday";i:1;s:9:"wednesday";i:2;s:6:"friday";i:3;s:8:"saturday"}
3 12 end_date 20141003
4 78 recurring_event 0
5 34 times 24 Hours
6 12 days a:2:{i:0;s:6:"monday";i:1;s:7:"tuesday";}
7 67 start_date 20140906
8 45 end_date 20141108
What MySQL queries can I use to get events for Today, Tomorrow, Weekend and 7 days.
I do not know SQL enough, then, for this case, I would caculate the date with PHP and then make the query with SQL.
These are the pages that helped me for the following :
http://php.net/manual/fr/function.date.php
http://php.net/manual/fr/function.mktime.php
1) For today, no calculation, just get the date of today and make the query :
<?php
$today = date('Y-m-d',mktime());
$result = mysqli_query($connect, 'SELECT * FROM events WHERE start_date = "'.$today.'"');
?>
2) Tomorow, calculate tomorow date :
<?php
$tomorow = date('Y-m-d',mktime(0, 0, 0, date("m") , date("d")+1, date("Y")));
$result = mysqli_query($connect, 'SELECT * FROM events WHERE start_date = "'.$tomorow.'"');
?>
3) 7 days later, calculate 7 days later date :
<?php
$day7 = date('Y-m-d',mktime(0, 0, 0, date("m") , date("d")+7, date("Y")));
$result = mysqli_query($connect, 'SELECT * FROM events WHERE start_date = "'.$day7.'"');
?>
3) Week end, calculte when week end comes :
For this one, I cannot write it so quickly, sorry.
Explanations :
date('Y-m-d',mktime()); gives 2014-09-04 in $today.
Because mktime() is empty, so mktime() is based on the server time, no argument.
Y => year like ####
m => month like ##
d => day like ##
date('Y-m-d',mktime(0, 0, 0, date("m") , date("d")+1, date("Y"))); gives 2014-09-05
This time we gave arguments to mktime(), 0 hour, 0 minutes, 0 second, 09, 04+1, 2014.
date("m") = 09
date("d")+1 = 04+1 = 05
date("Y") = 2014
I hope this might help you.
I'm sorry, but I don't know how put PHP in Wordpress.
Nils.
For the weekend and next 7 days you may have to mix sql and php
Today
SELECT * from tablename where start_date=CURDATE();
Tomorrow
SELECT * from tablename where start_date = CURDATE()+INTERVAL 1 DAY;
For Weekend you have to find the weekend dates first.
using
SELECT DAYOFWEEK
you can find current day . So if you have an array , match with it and
add how many days to reach Saturday and sunday.
I dont know any other easy way
For next 7 days
SELECT * from tablename where start_date >= CURDATE() and
start_date=< CURDATE()+INTERVAL 7 DAY;
Use the tutorial and try yourself :-)
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_curdate

Add 6 months to mysql field and subtract 1 week

I'm developping a web application and it has files from users that last 6 months . I want the application to give a warning to the system Administrator 1 week before they expire.
I have the following MySQL table
##lar_pis##
#id_plano nome_plano data#
1 Plano Individual 2013-02-22
2 Plano Individual 2013-01-04
3 Plano Individual 2013-02-22
4 Plano Individual 2013-01-20
5 Plano Individual 2013-02-22
6 Plano Individual 2013-02-22
So far I only have
SELECT *
FROM lar_pis
WHERE data = DATE_ADD(data, INTERVAL 6 MONTHS) ? AND SUBTRACT ONE WEEK ?
Try this way,
SELECT * FROM lar_pis WHERE data = DATE_SUB(DATE_ADD(data, INTERVAL 6 MONTHS), INTERVAL 1 WEEK)
You could do something like this:
SELECT *
FROM lar_pis
WHERE (data BETWEEN $date1 AND $date2)
Then define the $date1/2 variables with PHP date()?
I made it using PHP like this :
$hojemenosseis = date("Y-m-d", strtotime("-6 months"));
<?php while($rowpis = mysql_fetch_assoc($querypis)){
if($hojemenosseis == $rowpis["data"])
{ ... }

from date to date - search and display

I have a database holding two bits of information. from date to date. I need this entry to appear in my calender on every day, but I can only manage to get to appear on the first and last date.
example of DB:
job_no | date1(from)| date2(to)
________________________________
1 |2013-01-28 | 2013-02-03
2 |2013-01-14 | 2013-01-18
Edit for question. the search bar I have allows for ONE date input and the the calender finds entries through date1 and the next 6 days.
I cannot have a search which contains two date inputs because my users are so used to this way and i do not want to increase searching time. I started to think that I had to find the dates between the dates, add that to an array then use an if statement to find matches...but even saying this makes no sense to me.
regarding job 1, I need my calender to show this job up on all dates 28/29/30/31/01/02/03.
My current search SELECT * FROM jobs WHERE date1='$searchinput' PER day and calender this search. I use strtotime to increase the input date by +1 to add to the search for each day.
Calender page.
What I want with my results. User searched Date 28th.
Mon 28 | Tues 29 | Wed 30 ...... | Fri 03 |
_________________________________________________________________________
Job no 1 | job no 1 | job no 1 ...... | job no 1
What I have now.
Mon 28 | Tues 29 | Wed 30 ...... | Fri 03 |
_________________________________________________________________________
Job no 1 | blank | blank | job no 1
each day has a new select query right now. It matches days with date 1 and date 2. I dont need this as before I only had jobs out on one day now they go out for more than one day and need the job to be noted on all days it is out by only using a job from date and job to date.
EDIT 2:
SELECT * FROM calender_db NATURAL JOIN job_db
WHERE section='two'
AND date1 < '$day' AND date2 > '$day'
OR date1 = '$day' OR date2 = '$day'
This query selects what I need, but as I am using OR the first WHERE CLAUSE can be null. I need that to always be in the clause. I have been looking at using IIF or CASE to rectify but do not how to implement 100%...?
why not use BETWEEN
SELECT * FROM tableName WHERE date BETWEEN 'date1' AND 'date2'
SQLFiddle Demo
UPDATE 1
SELECT *
FROM tableName
WHERE date BETWEEN '2013-01-28' AND '2013-01-28' + INTERVAL 6 DAY
SQLFiddle Demo
To generate a list of days between two dates:
$days = array();
$stop = strtotime($date2);
for ($current = strtotime($date1); $current <= $stop; $current = strtotime('+1 days', $current)) {
$days[] = date('d', $current);
}
echo join('/', $days);
Demo
Update
Misunderstood the question it seems, if both dates are stored as columns and you're querying with a single date:
SELECT *
FROM jobs
WHERE 'date_from_input' BETWEEN date1 AND date2
Update 2
Your latest query can be written as:
SELECT *
FROM calender_db NATURAL JOIN job_db
WHERE section='two' AND '$day' BETWEEN date1 AND date2
Try like this
SELECT * FROM Mytimetable WHERE date BETWEEN 'date1' AND 'date2'
Query between date1 - date2
SELECT *
FROM `objects`
WHERE (date_field BETWEEN '2013-01-30 14:15:55' AND '2013-02-19 10:15:55')
OR
SELECT *
FROM `objects`
WHERE (date_field BETWEEN date1 AND date2)
query to select dates between two dates with PHP variables.
i will explain with exapmle :
$date1 = "2013-02-19";
$date2 = "2013-02-25";
then sql query will be :
$query = " SELECT * FROM table_xyz where (date_item BETWEEN date('".$date1."') AND date('".$date2."')";
hope it solves your problem
SELECT
*
FROM
tablename
WHERE
date between '$date1' and '$date2'
order by
date

grouping query result by month on the given period of time

hi im trying to get monthly sale of a online shop for a given period of time
i'm using unix epoch(time()) for storing sale date in the table
sold table looks like this
+-----------+---------------------+---------+----------+
| id | product_id | price | date |
+-----------+---------------------+---------+----------+
| 1 | 20 | 2000 |1323299365|
+-----------+---------------------+---------+----------+
| 2 | 28 | 3500 |1323297389|
+-----------+---------------------+---------+----------+
i want to findout monthly sale for a given period of time like
$from="3/2011";
$to ="4/2011";
i know how to change these dates to unix timestamp and get the result by something like that
//explode from
//$from = maketime(x,x,x,x,x);
//explode to
//$to= maketime(x,x,x,x,x);
$query = "select * from `sold_table` where date > $from and `date` < $to ";
but what i want is to group the results by month so i can have monthly statics
i know this query doesn't work but i want something like this
$query = "select sum(`price of each month`) from `sold_table` where date > $from and `date` < $to group by month ";
so i can have a result like
march(03) = 25000$;
april(04) = 200$ ;
i can group the result by every 30 days but i think there should be a cleaner way
maybe i can find out month between to epoch time like 1323299365 and 1323297389 and then get the monthly sale for each one of month ? but i dont know ho to extract month between two epoch time
Try something like this:
SELECT SUM(price), DATE_FORMAT(FROM_UNIXTIME(date), "%m") AS month FROM sold_table GROUP BY month
But be aware also of the year, you can use combination of month and year.

Categories