Count records this month - php

I have a table that stores dates in the format of MM/DD/YY (field name is date). I need to have a count that totals just the records for the current month. From searching, this is what I currently have but something is not right. Do I need to do a convertToDate, is it that my field is called "date", or am I missing something entirely?
$totalcount = mysql_query("select count(*) as 'total'
FROM state_to_state
WHERE status = 99 AND type = 1
AND MONTH(`date`) = MONTH(CURDATE())
AND YEAR(`date`) = YEAR(CURDATE())");
$totalnum = mysql_fetch_array($totalcount);
if($totalnum['total'] > 0) { $month_status = $totalnum['total']." this Month. "; }

Your dates are NOT native mysql dates (varchar, probably?), so you cannot use the mysql date functions on them directly. date('MM/DD/YY') will not work reliably:
mysql> select month('03/03/14'), month('03/18/14');
+-------------------+-------------------+
| month('03/03/14') | month('03/18/14') |
+-------------------+-------------------+
| 3 | NULL |
+-------------------+-------------------+
Convert your date fields to native mysql date types, or take a huge hit on performance and convert them to native date values onthefly via str_to_date().

Related

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);

Selecting a datetime data and getting only the day?

I have a datetime column in my database and want to echo out how many users regisetered today.
id | register_date
1 | 2014-04-16 09:55:13
2 | 2014-04-14 19:23:49
$sql_users = "SELECT * FROM users";
$result_users = mysql_query($sql_users);
I thought that I could check every single data in it by using date('d', $register_time ) in a while statement. Is there any easier way to do it?
Use MySQL's DATE() and CURDATE() functions:
SELECT COUNT(*) FROM users WHERE DATE(register_date) = CURDATE()

Comparing current date/time with date/time in a database

I need to create something to display an image based on the current date and time. I have an SQL table set up that looks like this:
eventid | startdate | enddate | imgfile | status
6 | 2013-04-29 | 2013-05-03 | finals.jpg | active
What I need to do is compare the start date and end date to the current date, and if the current date falls within the startdate and enddate, then display the imgfile.
I'm not sure how to go about this, but this is what I tried.
//get current date and time
$currentdatetime = strtotime(now);
$formatteddatetime = date("y-m-d", $currentdatetime);
while($row=mysql_fetch_array($getimage))
{
//get variable for startdate
$formattedstartdate = date("y-m-d", $row[startdate]);
$formattedenddate = date("y-m-d", $row[enddate]);
if($formatteddatetime >= $formattedstartdate AND $formatteddatetime <= $formattedenddate AND $row[status] == 'active')
{
echo $row[imgfile];
}
}
?>
Right now, it doesn't show anything inside of the entire while statement. Am I at least on the right track?
the CURRENT_DATE returns the current date in ‘YYYY-MM-DD’ format or YYYYMMDD format depending on whether numeric or string is used in the function. CURDATE() and CURRENT_DATE() are the synonym of CURRENT_DATE.
You should do it in your SQL query instead of PHP. For example:
SELECT * FROM table WHERE CURRENT_DATE BETWEEN startdate AND enddate;
CURRENT_DATE() is a MySQL function that returns the current date, as the name suggests. It is one of a few special functions that have an alias without the parentheses.

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.

PHP MySQL query- group by date in datetime field

I've been searching and I know there are similar questions, but none of them seems to answer this particular question.
I am trying to get a count of the total number of days an employee has worked on a given schedule. To do this, I am counting the total number of rows the employee appears on the "schedules" table. Only we run into a problem if the employee is scheduled twice on the same day.
To solve this, I want to count total number of rows and sort by DATE in a DATETIME field.
Current query:
$days = mysql_query("SELECT emp_id FROM schedules
WHERE sch_id = '$sch_id'
AND emp_id = '$emp_data[emp_id]'");
$tot_days = mysql_num_rows($days);
I would like to change it to:
$days = mysql_query("SELECT emp_id FROM schedules
WHERE sch_id = '$sch_id'
AND emp_id = '$emp_data[emp_id]'
GROUP BY start_date");
// "start_date" is a datetime field. Need to sort by date only YYYY-MM-DD
$tot_days = mysql_num_rows($days);
Any thoughts?
If your start_date column is a MySQL datetime type, you could use the following:
$days = mysql_query("SELECT start_date, count(*) FROM schedules
WHERE sch_id = '$sch_id'
AND emp_id = '$emp_data[emp_id]'
GROUP BY DATE(start_date)
HAVING count(*) > 1
ORDER BY DATE(start_date)");
The DATE function "Extracts the date part of the date or datetime expression"
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
This will give you only those rows where the emp_id being considered is used more than once in a given date. Remove the HAVING line if you want to see all.

Categories