Add 6 months to mysql field and subtract 1 week - php

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"])
{ ... }

Related

Increase days automatically in database

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.

Convert Date value to Accounting/Tax Year in mySQL

Is there anyway to convert a Date format value to a taxYear/Accounting year format, so that they could be then grouped by?
EG With Accounting/Tax year from April 01 - March 31 :
Date_Col Tax_Year/AccountingYear
2016-01-01 === 15/16
2015-05-25 === 15/16
2015-03-05 === 14/15
Is it possible within a mysql query, or a php function that can do the same job?
You can do the conversion by subtracting 3 months/adding 9 months and using something like:
select concat(date_format(date_sub(date_col, interval 3 month), '%y'),
'/',
date_format(date_add(date_col, interval 9 month), '%y')
) as tax_year
EDIT:
You can try this:
select concat(year(date_sub(date_col, interval 3 month),
'/',
year(date_add(date_col, interval 9 month)
) as tax_year
This version should return "2016/2017". I prefer 4-digit years in any case.

How to get all months with number of months and days from 2 date?

I have 2 tables :
a) clients
clients_id conn_date
=======================
1 2016-06-01
2 2016-07-17
3 2016-06-22
4 2016-09-03
b) clients_pay_bill
cpp_id clients_id paid_month
===================================
1 1 2016-07-03
2 2 2016-07-22
3 4 2016-09-09
4 2 2016-07-22
Now I want to show all months with number of days and months of which clients is not paid until current date.
For example :
clients_id = 1 connection date (conn_date) is 2016-06-01 and he only paid 2016-07-03 month bill till now. So the sql query will be output following months:
2016-06
2016-08
2016-09
**2016-07 will not print because he already paid this months bill**
and I also want to show number of days and months e.g: 3 months some days..
I can't imagine how the sql query should look like ?
Get the conn_date as well as paid_months for the client by executing
SELECT conn_date FROM clients WHERE client_id = <your_client_id>
And
SELECT paid_month FROM clients_pay_bill WHERE clients_id = <your_client_id>
Then you could use PHP to get an array of all the dates from conn_date to current_date.
Then foreach date check if it exists in paid_month array. If no, then display.
Here is the pseudo code
$start = (new DateTime(conn_date));
$end = date(Y-m-d);
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
if(!in_array($dt,$paidMonthArray)) {
display($dt(Y-m));
}
}

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

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

Categories