php graph is not fitting between the actual dates - php

my graph is not reading between the correct dates that the data was inserted and instead its reading the amount of rows and displaying all the 29 days. Here is the code:
$graphTimeOne = time();
for($graphTimeOne = 29; $graphTimeOne >= 0; $graphTimeOne--){
$sel_timestamp = mktime(0, 0, 0, date("n"), date("j") - $graphTimeOne, date("Y"));
$graphResult = mysql_query("SELECT COUNT(id) FROM user WHERE timestamp >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 29 DAY))");
Basically its suppose to display how many are per day, but instead it displays the total on every day, the interval is 29 days. Any ideas how I can fix it?

Your SQL is incomplete - it is missing the day grouping:
SELECT COUNT(id)
FROM user
WHERE timestamp >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 29 DAY))
GROUP BY DAY(FROM_UNIXTIME(`timestamp`))

Related

count where different is more than 2 days

Hi i have this code that I would like to get the total of log that is closed.
(A) More than 2 days.
(B) Less than 2 days.
for($m=1; $m<=12; ++$m) {
$next_year = $cur_year + 1;
$monthName=date('M', mktime(0, 0, 0, $m, 1)).'';
$monthNumber = date('m', mktime(0, 0, 0, $m, 1));
$monthName = date('F', mktime(0, 0, 0, $m, 1));
$currentMonthText = date('Y-m-d', mktime(0, 0, 0, $m, 1));
$nextMonthText = date('Y-m-d', mktime(0, 0, 0, $m + 1, 1));
$result= $DB->query("SELECT * FROM ".$DB->prefix("calldesk_log")."
WHERE MONTH(date_open)='$monthNumber' AND date_open >= '$currentMonthText'
AND date_open < '$nextMonthText' AND status='Closed'") or die(mysql_error());
while($row = $DB->fetchArray($result))
{
$id_report=$row['id_report)'];
$date_open=$row['date_open'];
$date_close=$row['date_close'];
$diff = abs(strtotime($date_open) - strtotime($date_close));
$min = floor($diff / (60*60*24));
}
}
Any help will be appreciated.
If its only a matter of days differences, let MYSQL do the job using DATE_ADD() Function, you will thus avoid classical date mistakes which are obvious in your actual code (leap years, year change, etc...)
Here are some examples depending on what you want to do , which is unclear...
Logs that are between 2 days in the past and 2 days in the future (if that's possible but this what you do in your code)
SELECT *
FROM ".$DB->prefix("calldesk_log")."
WHERE date_open BETWEEN DATE_ADD(curdate(), INTERVAL -2 DAY)
AND DATE_ADD(curdate(), INTERVAL 2 DAY)
AND status='Closed'
Logs that are between current date and 2 days in the future
SELECT *
FROM ".$DB->prefix("calldesk_log")."
WHERE date_open BETWEEN curdate()
AND DATE_ADD(curdate(), INTERVAL 2 DAY)
AND status='Closed'
Logs that are between 2 days in the past and current date
SELECT *
FROM ".$DB->prefix("calldesk_log")."
WHERE date_open BETWEEN DATE_ADD(curdate(), INTERVAL -2 DAY)
AND curdate()
AND status='Closed'
If you want to count months instead of days, just change the INTERVAL:
DATE_ADD(curdate(), INTERVAL -2 MONTH)
If you want to take the time into account, use NOW() instead of curdate():
DATE_ADD(NOW(), INTERVAL -2 DAY)
That's it, you can do all this in pure SQL and let MySQL do the job for you.

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

selecting all rows 7 days from now based on unix timestamp and current date

I have a table with the following fields:
id - int
name - int
the_date - int
the_date is a unix timestamp for when the row was added
Now I am trying to write a query that will select all rows on the day that is 7 days from now. I'm not talking about select the rows >= 7 days from time(), I need to grab the current day using time(), and then run a SELECT that grabs all the rows that were inserted on the day that is 7 days from the time().
I know how to do it so its within 7 days from the time() with a simple >= SELECT, but I don't know how to do it so it selects all rows whose unix timestamp is on that particular day (7 days from now).
Any help would be greatly appreciated.
The points of intrest here,
i use curdate() to get the current DATE, not DATETIME.
i add 7 days and convert to unix time, which yields the starting second of that day
i do the same for the next day
i structure the where clause to be >= or equal to the target day, but < the start of the next day. This gives a range of seconds that fully covers the target day.
i dont use any functions on the column itself. This is important because if i did, mysql wouldn't be abl;e to use any indexes that exist on the column to fullfill the query.
where the_date >= unix_timestamp(curdate() + interval 7 day)
and the_date < unix_timestamp(curdate() + interval 8 day)
SELECT * FROM `dbname1`.`test`
WHERE
date(FROM_UNIXTIME(`the_date`)) = ADDDATE(DATE(NOW()), INTERVAL 7 DAY);
$todayParts = getdate();
$startToday = mktime(0, 0, 0, $todayParts['mon'], $todayParts['mday'], $todayParts['year']);
$sevebDaysFromNow = 60 * 60 * 24 * 7;
$seventhStart = $startToday + $sevebDaysFromNow;
$seventhEnd = $seventhStart + 60 * 60 * 24;
$sql = "SELECT * FROM <table> WHERE the_date BETWEEN $seventhStart AND $seventEnd";
This will calculate 7 days from the start of the day you are now. Hope this helps

MYSQL get data within an certain time frame

hey everyone,
having problem getting this MYSQL query correct
SELECT *
FROM tbl_problem
WHERE user_id = 1
AND problem_solved != -1
AND problem_timestamp BETWEEN '20110212' AND DATE('20110212', INTERVAL 14 DAY)
ORDER BY problem_id
the problem is 20110212 is dynamically generated by php, which is why i use the DATE/INTERVAL COMBO.
what i am trying to do is select entries within a 2 week time frame given that you know the start date of that two week cycle.
thanks in advance
Why not just get PHP to reformat it and then query for
SELECT *
FROM tbl_problem
WHERE user_id = 1
AND problem_solved != -1
problem_timestamp > '2011-01-24' AND problem_timestamp < DATE_ADD('2011-02-12', INTERVAL 14 DAY)
ORDER BY problem_id
To convert the date in PHP from your current format to MYSQL's format, use
$date = date_create_from_format('Ymd', '20110212');
$mysqlDate = date_format($date, 'Y-m-d'); //2011-01-24
Use Datediff.
SELECT *
FROM tbl_problem
WHERE user_id = 1
AND problem_solved != -1
AND DATEDIFF(problem_timestamp,'20110112') <= 14
ORDER BY problem_id

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