Convert Date value to Accounting/Tax Year in mySQL - php

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.

Related

How to get last 3 days records from database without today?

I am using this code:
WHERE date > DATE_SUB(CURDATE(), INTERVAL 3 DAY)
And result will be like this:
2022-05-12
2022-05-11
2022-05-10
But I want this:
2022-05-11
2022-05-10
2022-05-09
Use a range here:
WHERE date < CURDATE() AND date >= DATE_SUB(CURDATE(), INTERVAL 3 DAY)
Assuming today's date be 2022-05-12, the above logic would exclude this date but include the three previous days, from 11th May to 9th May.

How to filter last one week data on a specific day

Hi I have a MySQL database that already has auto increment date time colomn (may information help).
How to filter last one week data as start from specific day until next 7 day.
example. Show data from Wednesday on this week until nest 7 day
$query="SELECT * FROM diagnosa WHERE schedule= '$a'";
$a is start day the data start to be shown (example Wednesday)
This may help you...
SELECT *
FROM table
WHERE date_colunm between 'start_date' AND DATE_ADD("start_date", INTERVAL 7 DAY);
UPDATE :
As you need from the Wednesday. Please us below query
SELECT *
FROM table
WHERE date_colunm
between (
CASE
WHEN DAYOFWEEK(curdate()) > 4
THEN DATE_SUB(NOW(), INTERVAL (7-DAYOFWEEK(curdate())) DAY)
ELSE (
CASE
WHEN DAYOFWEEK(curdate()) < 4
THEN DATE_ADD(NOW(), INTERVAL (4-DAYOFWEEK(curdate())) DAY)
ELSE curdate()
END
)
END
) as date
AND DATE_ADD (
(
CASE
WHEN DAYOFWEEK(curdate()) > 4
THEN DATE_SUB(NOW(), INTERVAL (7-DAYOFWEEK(curdate())) DAY)
ELSE (
CASE
WHEN DAYOFWEEK(curdate()) < 4
THEN DATE_ADD(NOW(), INTERVAL (4-DAYOFWEEK(curdate())) DAY)
ELSE curdate()
END
)
END
) as date, INTERVAL 6 DAY
)
NOTE :
There are some codes for the days like for Wednesday we have 4 (as mentioned in query),
so you can change the code according to your requirements. Like for Monday is : 2

Get the dates of 4 quarters mysql

I want the start and end dates of 4 quarters, so that I can use BETWEEN function to count how much data added in each quarter. How do I get the dates?
$this->db->select('SUM(CASE WHEN(data.added_on BETWEEN MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE())-2 QUARTER AND MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE())-1 QUARTER - INTERVAL 1 DAY ) THEN 1 ELSE 0 END) data_quarter3');
this will work for the previous quarter, but I don't want this. Any solution is welcome.
You don't need the dates, just specify the quarter and year you want in your WHERE statement
WHERE QUARTER(data.added_on) = 2 AND YEAR(data.added_on) = YEAR(NOW())

PHP Count Between Date and Todays Date

In my dates database I have a table of dates that have two followups to be completed, one after 30 days, one after 60 days. I need to build a page that uses the MySQL query to pull all dates from the dates table that have a 30day value of No (which I can do). Now the tricky part is, I need it to only output the dates that meet that criteria, and are 30 days from the current date.
For example: August 4 & 6 have a 30day value of No, August 5 has a 30day value of Yes. Today's date is September 4. 30-days prior would be August 5.
I need the query to only display August 4 in this case, since it hasn't been 30 days since August 6 and August 5 has already been done.
I am unsure what kind of function to use to do this counting. I appreciate your help
EDIT:
Date - 30day Value
July 1 - Yes
July 5 - No
August 1 - No
August 5 - No
August 6 - Yes
Today's Date is September 2.
The table would display July 5 and August 1, as their 30day values are No, and they are more than 30 days from todays date.
You should use DATEDIFF function:
SELECT ....
FROM your_table
WHERE DATEDIFF(CURDATE(), event_date) = 30
Where event_date is example of your date column.
MySQL's DATEDIFF function allows you to subtract 2 dates in a query.
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_datediff
DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.
For example:
SELECT some_id, date_column
FROM date_table
WHERE DATEDIFF(CURDATE(), date_column) = 30
You could also select both 30 and 60 days like this and also have a cutoff date of 60 days so it's not searching the whole table:
SELECT some_id, date_column
FROM date_table
WHERE date_column>=DATE_SUB(CURDATE(), INTERVAL 60 DAY)
AND DATEDIFF(CURDATE(), date_column) IN (30, 60)
And since I'm making some assumptions with my understanding of what you're asking, you may also want to do this which will return the results as 'Yes' or 'No' in your result set:
SELECT some_id, date_column,
CASE DATEDIFF(CURDATE(), date_column)
WHEN 60 THEN 'Yes'
WHEN 30 THEN 'Yes'
ELSE 'No'
END CASE AS is_3060_day
FROM date_table
WHERE date_column>=DATE_SUB(CURDATE(), INTERVAL 60 DAY)
Alternatively if you want to accomplish this on the PHP side, you could use PHP's date_diff function:
http://php.net/manual/en/function.date-diff.php
function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval->format($differenceFormat);
}
$result = dateDifference($date1, $date2)
if ($result==30 || $result==60) {
// Do something
}
you can fetch both the dates and use the php function
$prevdate = date_create("2013-03-15");
$currdate = date_create("2013-12-12");
$diff = date_diff($prevdate,$currdate);
echo $diff->format("%R%a days");
Output
272 days

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

Categories