Given date as parameter retrieve month and year - php

In PHP, I want the system to automatically get the fixed date (14th) regardless of any time I request.
Question:
So do I need to write a custom function for this matter or are there any built in function that I can use?
I've also provided some example as below:
A=Current Date(input)
B=System output
Scenarios
Case 1
A: 2017-03-30
B: 2017-03-14
Case 2
A: 2017-05-31
B: 2017-05-14
Case 3
A: 2017-06-03
B: 2017-05-14
PHP
$fulldate = date("Ymd");
$year = substr($fulldate,0,4);
$month = substr($fulldate,4,2);
$date = substr($fulldate,6,2);
if($date <= 14){
$month -= 1;
}
echo $year.$month.'14';
MySQL
select * from table where DATE_FORMAT(my_date,'%Y%m%d') = $year.$month.'14';

You can get the "last 14th of a month" in SQL with
select concat_ws('-', year(now()), month(now()), 14) - interval (day(now()) <= 14) month
The "trick" ist this part:
- interval (day(now()) <= 14) month
which will substract one month only if the current day is <= 14
So you can use this query
select *
from `table`
where my_date = concat_ws('-', year(now()), month(now()), 14) - interval (day(now()) <= 14) month
without doing anything in PHP.
Using PHP you can do it the following way:
$date = new DateTime();
$date->modify('-14 day');
$last14th = $date->format('Y-m-14');
echo $last14th;
$sql = "select * from `table` where my_date = '{$last14th}'";
Update
Looking at the PHP code I realized, that the same logic can be used in SQL:
select date_format(now() - interval 14 day, '%Y-%m-14')

you may use this:
select DAY(DATE_FORMAT("2017-06-03",'%Y-%m-%d'));
+-------------------------------------------+
| DAY(DATE_FORMAT("2017-06-03",'%Y-%m-%d')) |
+-------------------------------------------+
| 3 |

Related

consulting database when data is today, tomorrow, after tomorrow

I need to consult a table using mysql...for today, tomorrow, after tomorrow,..
by now I use the following code where data is the day in format YYYY-MM-DD
$data_query = mysqli_query($conexionbd,'select * from `Meteo` where `data` >= "2017-03-31" ');
what should I insert in where data >= in order to get the data for today, tomorrow...without inserting manually the date?
You can it easy calculate with:
SELECT DATE (NOW() + INTERVAL 1 DAY);
sample
mysql> SELECT DATE (NOW() + INTERVAL 1 DAY) as result;
+------------+
| result |
+------------+
| 2017-03-30 |
+------------+
1 row in set (0,00 sec)
mysql>
To get tomorrow's date:
$tomorrow = date("Y-m-d", strtotime("+ 1 day"))
Now you can add that variable to your query:
$data_query = mysqli_query($conexionbd,'select * from `Meteo` where `data` >= "'.$tomorrow.'" ');
Then you can simply add more days as needed:
$after_tomorrow = date("Y-m-d", strtotime("+ 2 day"))
$DTtoday = new DateTime(); //Current date
$DT2days = clone $DTtoday; //Create new object from $DTtoday
$DT2days->add(new DateInterval("P2D")); //Add 2 days
$data_query = mysqli_query($conexionbd,'select * from `Meteo` where `data` BETWEEN "'.$DTtoday->format("Y-m-d").'" AND "'.$DT2days->format("Y-m-d").'"');
You could use now()
and between interval 2 days
data_query = mysqli_query($conexionbd,'select *
from `Meteo`
where `data` between now() and date_add(date(now() ), interval 2 day)) ;

year and month in where clause in oracle 10g

I have my date in format March 2014. How do I get month and year in number format to compare it in Oracle 10g query.
<?php
$date = "March 2014";
?>
My query that I am trying to get is
select EMP_NAME where month = "03" and year = "2014".
Here is my table structure
EMP_ID | EMP_NAME | C_DATE
1 | ABC | 01-FEB-14
2 | XYZ | 03-MAR-14
Note: I am writing the coding in php
For a sargable predicate you should avoid altering every row of data to suit the single parameter.
Instead: Adjust the parameter to suit the data
select
*
from your_table
where C_DATE>= to_date('March 2014','MON YYYY', 'NLS_DATE_LANGUAGE = American')
and C_DATE< add_months(to_date('March 2014','MON YYYY', 'NLS_DATE_LANGUAGE = American'),1)
Note the data - C_DATE - is not affected by any function, yet you get the desired outcome (all records of March 2014); this method permits use of indexes on the field C_DATE for query efficiency.
You can try with TO_CHAR(date, 'MON') for the months and TO_CHAR(date, 'YYYY') for the year.
The query will be
select EMP_NAME where TO_CHAR(C_DATE, 'MON') = month and TO_CHAR(C_DATE, 'YYYY') = year
And in php you'll have to manage the date to get year and month and pass them to the query
$dt = new DateTime("March 2014");
$month = $dt->format("m"); //03
$year = $dt->format("Y"); //2014
//Here connect to the db..
$db->query("SELECT EMP_NAME FROM yourTable WHERE TO_CHAR(C_DATE, 'MON') = :month AND TO_CHAR(C_DATE, 'YYYY') = :year");
$db->bindParam(":month", $month);
$db->bindParam(":year", $month);

Calculate subtracts date in mysql for birthday event

i create a event for calculate a birthday , for example:
birthday date = 1990-09-07
now date = 2013-09-05
my query :
SELECT id FROM user WHERE ( birthday - NOW() ) <= 7
this query is mistake
I think DAYOFYEAR function is more suitable for you:
mysql> SELECT DAYOFYEAR('2000-09-07') - DAYOFYEAR(now()) AS diff;
+------+
| diff |
+------+
| 3 |
+------+
Today is 2013-09-05, it gave 3 days. Now you can compose the condition. Please, mind 1 day in the leap year.
You said its for birthday so you must consider month and date as date can be repeat for every month..
So try something like below, for year difference.
SELECT
(YEAR(birthdate) - YEAR(NOW())) AS yeardifference
FROM
table
WHERE
MONTH(birthdate) = MONTH(NOW())
AND
DATEDIFF(birthdate, NOW()) <= 7
For day difference
SELECT
DATEDIFF(birthdate, NOW())
FROM
table
WHERE
MONTH(birthdate) = MONTH(NOW())
AND
DATEDIFF(birthdate, NOW()) <= 7
The Dayofyear function would be useful, just to put it in context with your requirement
SELECT id FROM user WHERE (DAYOFYEAR(birthday) - DAYOFYEAR(NOW())) <= 7
I think your query should be something like
SELECT id
FROM user
WHERE ( DAYOFYEAR(birthday) - DAYOFYEAR(NOW()) <= 7)

MySQL - Between months (rather than a timestamp)

how would I generate a MySQL query to grab a row that is between a particular month
eg grab row where month is between 01/01 (1st Jan) and 01/06 (1st June)
I'm not sure, if you want to grab all rows from Jan to June, only the rows until June 1st or all rows from Jan to May. You can use for example:
[...] WHERE `date_column` BETWEEN '2012-01-01' AND '2012-06-01'
which gives you all rows including June 1st.
Use this to get all rows from the full months of Jan to June:
[...] WHERE YEAR(`date_column`)=2012 AND MONTH(`date_column`) BETWEEN 1 AND 6
(change to BETWEEN 1 AND 5 for Jan to May)
If you generate your SQL query in PHP, you can use BETWEEN
$date = date("Y") . '-' . date("m") . '-01';
$sql = "SELECT *
FROM `table`
WHERE `date_column` BETWEEN '$date' AND '$date' + INTERVAL 1 MONTH - INTERVAL 1 DAY";
(I subtracted one day to exclude the data of the next month first day)
Or you can use EXTRACT
$month = 5;
$sql = 'SELECT *
FROM `table`
WHERE EXTRACT(MONTH FROM `date_column`) = ' . $month;
Assuming the column in mysql is the right format (date/datetime) you can simply compare (>,<) :
SELECT somecolumn
FROM sometable
WHERE datecolumn >= '2012-01-01' AND datecolumn < '2012-06-01'
Hope it helps!
You can use the month(), year() and day() functions. So your query would look like something like
SELECT * FROM table WHERE month(date_field) >= 1 AND month(date_field) <= 6 AND year(date_field) = 2011
date_field should be a date or datetime field, if it is a timestamp, you need to use the FROM_UNIXTIMESTAMP function so it would look like month(FROM_UNIXTIMESTAMP(timestamp_field))
SELECT
*
FROM
yourtable
WHERE
date BETWEEN '2012-01-01' AND '2012-06-01';

Select all the records of the previous month in Zend Framework?

I have a small requirement in my project:
I want to fetch all the records of the previous month from the database.
The structure of the table is as follows:
id clientid task date
1 1 1 01.Feb.2011 12:13
2 1 1 05.Feb.2011 15:22
3 1 0 09.Feb.2011 14:17
4 2 1 11.Feb.2011 19:53
5 1 0 19.Feb.2011 14:27
6 2 1 23.Feb.2011 09:53
7 1 0 01.Mar.2011 14:17
8 2 1 01.Mar.2011 19:53
9 1 0 03.Mar.2011 14:67
10 2 1 03.Mar.2011 09:53
.....................
Here I want to fetch all the records of the previous month of a particular client in Zend Framework.
For Example : If I want client 1 records then It should show me records : 1,2,3 and 5.
Please Suggest some code, or link that helps me......
Thanks in advance
Assuming the date column is a DateTime column, I'd try with something like
$select->from('tablename')
->where('MONTH(date) = ?', date('n', strtotime('last month')))
->where('YEAR(date) = ?', date('Y'))
->where('clientid = ?', $clientId)
;
Note: untested and likely needs tweaking but it's the general direction
This would fetch all rows from tablename where the month is the last month and year is the current year and your clientId is the selected clientId. So the query should become something like
SELECT * from tablename
WHERE MONTH(date) = 2
AND YEAR(date) = 2011
AND clientid = 1;
You could also put the calculation for last month and current year directly into the query, e.g. using the appropriate MySql functions for this instead of calculating them with PHP. This might be more reliable.
You can get the first day of the current month using PHP:
$this_month = mktime(0, 0, 0, date("m"), 1, date("Y"));
$previous_month = mktime(0, 0, 0, date("m")-1, 1, date("Y"));
Then you simply pass this date as a parameter of your query:
SELECT * FROM mytable WHERE date >= ? AND date < ? and client_id = 1
where you replace the ? respectively by '$previous_month' and '$this_month'
If your date field is of the type Datetime you can use the date specific functions in MySQL to do this. Simply construct your statement with Zend_Db_Expr when using database functions.
My Zend_Db skill is a bit rusty, but I think the following does what you want:
$select->from('tablename')
->where(new Zend_Db_Expr('MONTH(`date`) = MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH))'))
->where(new Zend_Db_Expr('YEAR(`date`) = IF(MONTH(NOW()) = 1, YEAR(NOW()) - 1, YEAR(NOW()))'))
->where('clientid = ?', $clientId)
you can use SQL DATE_SUB and INTERVAL function like:
select * from table where `date` >= DATE_SUB(NOW(), INTERVAL 1 month)
In ZF1 you can write something like:
$select->from('tablename')
->where( 'date >= DATE_SUB(NOW(), INTERVAL 1 month)');

Categories