selecting and displaying rows from different date values - php

I want to get rows from a table based on different dates. Although values of the dates are not always the same.
Basically I have to get first what are the range of the dates and then extract the rows of the columns based on those dates.
I cant do SELECT COUNT(location) FROM tweets WHERE date<= NOW() or date<=2012-07-29 17:38:32
cause as I said dates are not fixed. I DON'T PUT THE DATES but they are dynamically created.

Well, the crux of this is how the dates are generated but the query is simple:
$start = '2012-6-24';
$end = '2012-7-24';
"SELECT COUNT(location) FROM tweets WHERE date between '$start' and '$end'";

Related

MYSQL Query - Sort by Date in PHP

I'm trying to set up a query that will search a MYSQL database and only pull in the rows from the database who's expiry_date is after todays date.
I would also like to be able to work out how many days or weeks there are remaining from todays date to the expiry date of the rows in the database that match the above query.
I think that in order to get the current date I would have to set up a variable of $date = time(); which I will then later be able to use to compare against the expiry_date column in the database. However I am now stumped as what to do to achieve the required result. I'm not exactly a PHP noob but I'm not an expert either, so please go easy on me ;)
Thanks in advance!
If the Column you want to check is a DATE(TIME), try
$sql="SELECT column FROM table WHERE expiry_date > CURDATE()";
If you saved the UNIX timestamp, you can simply use
$sql="SELECT column FROM table WHERE expiry_date > '".time()."'";
If you use the first with "NOW()" or the second, you'll proably get results for the current day.
If this is not acceptable, try "mktime(0, 0, 0)" instead of time();
Use this query
$query = "select timestampdiff(days,'$exipry_date','$now')";

Returning a report for each date in a PHP array

What I am trying to do here is render a report for each date in the current month from a MYSQL table.
I have a table with rows in, which a lot have the same date. As you can see here
What I would like to do is for every date in the current month is see how many rows have been found in the MYSQL table for that date and then return it into an array. The final product will be a multidimensional array for every date in the current month. Something like this
array("dates"=>
array(
"1 feb"=>2, //the number of rows for that date found in the MYSQL table
"2 feb"=>8,
"3 feb"=>0
)
)
But the issue is I wouldn't have a clue where to start with the coding, like what PHP functions would I use? So I was hoping someone could push me in the right direction :)
Probably not working code but should point you in the right direction:
SELECT
SUM(money) AS sum,
`date`
FROM
money_table
WHERE
MONTH(`date`) = 2
GROUP BY
DAY(`date`)
Reference
GROUP BY
MONTH
DAY
You said you didn't have a table with all the dates. If you did, this would be trivial. Let's say you had such a table called calendar.
select calendardate
, ifnull(sum(money), 0) sum_of_money
from calendar left join money on calendardate = money.date
where calendardate >= {d '2013-02-01'}
and calendardate < {d '2013-03-01'}
group by calendardate
Other things that could be stored in the calendar table are fiscal information and holidays. It might be worth your while to create and maintain one.

Sorting through a Database query for all rows created in the same day Unix Timestamp in PHP

I am developing a PHP application which will handling many company articles.
Now I am creating a page which should order articles BY DATE ie all articles created in a certain day are shown with the appropriate heading and so on for all the articles.
I have used Unix timestamps to save the dates in the MySql db but I cant find code which only sorts dates by days. Can I please get some assistance.
Thanx
You can use mktime() to get the timestamps you would want to use to bin the entries:
http://us.php.net/manual/en/function.mktime.php
$timestamp_for_my_birthday = mktime(0,0,0,10,16,1984);
$timestamp_for_the_next_day = mktime(0,0,0,10,17,1984);
if($time > $timestamp_for_my_birthday && $time < $timestamp_for_the_next_day){
// Time is on my birthday
}
To make this into a MySQL query:
$SQL = "SELECT * FROM dates WHERE date > $timestamp_for_my_birthday AND date < $timestamp_for_the_next_day;";
To order by date I think it would go something like:
SELECT * FROM dates ORDER BY FROM_UNIXTIME(time, '%M %d %Y');
Can you do a
SELECT DATE(FROM_UNIXTIME(my_field_name)) FROM table_x ORDER BY my_field_name
You can order by the timestamp column, but if you want a string representation of which day the record belongs to, you can get any part of the date with the FROM_UNIXTIME(timestamp, 'format') function.
select FROM_UNIXTIME(timestampColumn, '%Y %m %d')
The format values in the second parameter can be adjusted based on the table here:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

MYSQL retrieve results where date = next weekend

Currently I have two tables and the following sql statement which correctly retrieves the items in the events table ordered by their dates in the event_dates table:
SELECT * FROM events, event_dates
WHERE events.id=event_dates.event_id
AND events.preview=0 AND event_dates.start_date>=now()
ORDER BY event_dates.start_date ASC,event_dates.start_time ASC LIMIT 3
Now I want to add an extra AND to make sure only the events on the next weekend are set. The date column is in a standard mysql date format (YYYY-MM-DD). Got stuck on this bit. Cheers.
Use PHP strtotime() to get the start and end timestamp of the weekend:
$we_start=strtotime('next saturday');
$we_end=strtotime('next monday')-1;
Then do a sql query to search for timestamps BETWEEN them.
select * from mytable where UNIX_TIMESTAMP(mydatefield) BETWEEN $we_start AND $we_end
Hope that helps.

Selecting the next date in MySQL

I have a list of dates in a table in a MySQL database (the dates when a charity bookstall is to be held), which I want to display on a page. On one page I'm displaying the date of the next stall, and on another the dates of the stall in the next month. (Currently I'm using an unordered HTML list and selecting the dates with PHP, but it's a bit messy, and I also want to tie in the dates with the fundraising totals that are stored in the database).
I want to put the dates in a database though so that I can tie in the dates with the fundraising totals for each week. I'm thinking that once I can identify the date with the nearest up-coming date that I can use 'LIMIT 1' to select the next week's date for display, and 'LIMIT 4' say for where I need to display the dates for the next month, but what I can't figure out is how to identify the record with the nearest up-coming date - identifying the current date and then selecting the nearest date...I have a feeling there's probably one of the MySQL date functions that can be persuaded to help out in this, but can't figure out exactly how.
Any ideas on how I can do this?
If I understand correctly, you can just pick up next four dates that are after today.
In MySQL you could use the CURDATE() function for the 'today' bit, then apply an order and limit to your select statement. For example,
SELECT stall_date
FROM stall_dates
WHERE stall_date >= CURDATE() -- >= assumes you want today's to show too
ORDER BY
stall_date
LIMIT 4
Use ORDER BY stall_date DESC to reverse the ordering if needed.
If your column is a DATETIME field, you can identify the next by using SELECT...WHERE event_date > "2009-11-06" and ORDER BY event_date.
SELECT * FROM so_events
WHERE event_date > "2009-11-06 15:36:00"
ORDER BY event_date ASC
LIMIT 4
MySQL will internally do the work for you and select rows where whose timestamp is greater than the one you specify in the WHERE clause.

Categories