How to select data based on months using mysql - php

I'm a beginner in php and making a small online banking system in php for my College project. I wanted to generate the statement for each month. I'm recording each transaction happening in each days. I wanted to retrieve rows from the table till the end of each month beginning from the starting of the account. So If I click on the view statement link, (considering that I opened the account on 14th of Feb) I need to see two statements. 1 from 14th of Feb to 28th of Feb and another one from 1st of March to current date.
What mysql query should I used to get this?

You might try appending to your current query the condition for the current month, you can -1, -2, -3, etc... to the AND MONTH... condition.
AND
YEAR(`date_column`) = YEAR(NOW())
AND MONTH(`date_column`) = MONTH(NOW())
where date_column is the name of the column you are working. It might also be that you are trying to GROUP BYMONTH(date_column)` its a bit hard to tell.

Related

MySQL grouping by season

Im trying to figure out the most efficient way of calculating statistics using data from MySQL database with dates.
Currently, I use the following syntax
Example:
SELECT sum(Precipitation) from DataTable GROUP BY YEAR(Datetime)
This works perfectly fine, I get the total rainfall for each year. However, now I would like to implement the option to set the beginning of the rain season. In some places, the rain season might begin for example in September. In such case I would need the same calculation, i.e. also grouped by "years", but always since Sep to Aug.
I was thinking about how to do this and the only way I can think of would be somehow calculating the monthly sums and the using PHP try to add them up. But the problem is that that would probably be much slower given there is lots of data and the original script uses just this one line above.
Is there any more efficient way of then getting something like
2014 - xyz inches, 2015 - xyz inches, but where the 2014 would correspond for example to season 2014/2015 etc.
The data in the table is like this: column 1 is always the Datetime and then the actual value, data in 5 minute intervals. I need to maintain the table structure, so I cannot create a different table where the values would be organized differently.
Use this query:
SELECT SUM(Precipitation)
FROM DataTable
GROUP BY YEAR(DATE_SUB(Datetime, INTERVAL 8 MONTH))
This query shifts every date backwards by 8 months, with the result that September 1, 2016 would appear to be the first day of 2016, and August, 2016, would appear to be the last month of 2015.

PHP MySQL Table w AJAX: Filling in data for the whole year (day by day, only showing 7 days at once)

I’m struggling on how to design this small application. It’s going to be a table info where the columns will be dates and only 7 dates will be shown at any given time (the current day, plus the next six days).
The user NEEDS to be able to scroll to the left (or right, I suppose) and see previous dates with information that has bee entered for specific dates (ie, user wants to see the # of Service Jobs for February 14th, 2015).
What I have so far:
In this case there are 6 rows of information. The user can go in and edit this information. All of this data needs to be posted to a database, and the dates need to account for the whole year (ie, 1/1 to 12/31) and this has to happen every year. I’ve worked with the x-editable plugin a bit and have had some success, but I’m having a difficult time understanding how to account for all the differernt days of the year (365) and how to incorporate that into a MySQL database.
Current'y the 6 dates you see at the top of the img are shown using Javascript. The little dotted lines below the row values are from the X-editable plugin, and when you update this info it is posted to a database with a timestamp.
So my question is: how in the world do I account for 365 days for the year? It's not like I want 365 columns, each representing one day of the year, that seems crazy. (and the same thing will have to happen for 2016, 2017, etc etc...ongoing data).
I'm not sure if I've been taking the right approach on this. Feel like I'm making it much more painful than it needs to be.
Any input on how to design such an application would be greatly appreciated.
From a high level; I would just use an indexed DATE column.
A quick example:
<?php
$date_start = date('Y-m-d'); // Today
$date_end = date('Y-m-d', strtotime($date_start . ' + 6 days'));
// Assuming you have a PDO DB handler setup at this point...
$stmt = $dbh->prepare('SELECT * FROM `table` WHERE `day` BETWEEN ? AND ? ORDER BY `day` ASC');
$stmt->execute(array($date_start, $date_end));
$res = $stmt->fetchAll();
var_dump($res);

Replace DAY at end of Date

I have a column in MySQL stored as: 2014-03-16 (for March 16, 2014). It has been decided that the fields should all be changed to the first day of the month -- whatever months in which they occur. I have records that span from 1998 to present day, several thousand of them. Is there an easier way to change just the DAY (in this case, 16) on all the records besides REPLACE 2014-03-16 with 2014-03-01?
I am using PHP, so if it's easier to change it there let me know.
You can do it using a MySQL query:
UPDATE your_table
SET date_column = DATE_FORMAT(date_column ,'%Y-%m-01')

Help with a MySQL Unix timestamp Query

I am trying to create a custom MySQL for use with the Expression Engine CMS. The purpose of the query is to display events that are happening today or in the future.
The problem is that the EE field type that allows you to put in the date and converts it into a unix timestamp. If I pick the 26th July it puts in the date value "25th July 23:00".
As you see from my query below it almost works but I need to add 24 hours onto the values that are used in the conditional part of the statement. I want events that occur on the day "for example today 25th July" to be displayed up until 23:00 hours that day then be removed.
I almost have it I am just stuck on how to add 24 hours to the conditional.
SELECT t.entry_id,
t.title,
t.url_title,
d.field_id_13 AS event_lineup,
d.field_id_14 AS event_details,
d.field_id_15 AS event_day,
d.field_id_16 AS event_flyer_front,
d.field_id_17 AS event_flyer_back,
d.field_id_18 AS event_facebook,
d.field_id_12 AS event_date
FROM `exp_weblog_titles` AS t
NATURAL JOIN `exp_weblog_data` AS d
WHERE d.weblog_id = 5
AND CAST(d.field_id_12 AS UNSIGNED) >= (unix_timestamp(Now()))
ORDER BY d.field_id_12 ASC
What I think might be happening is your timestamps get adjusted for the time zone, and that adjustment is configured differently in the CMS and on the server.

Archive Builder PHP

Before i start id like to say ive posted this question as more of a discussion rather than Problem Question.
In my Database i have news posts lets say with 3 columns (Id, title, date). Wher Id and title are self Explanitory the date is stored in mktime() values, in other words the number of seconds passed since 1 January 1970.
Now what i want to do is build an archive link that will display as such
July 2009
June 2009
March 2009
Feburary 2009
December 2008
Note the months on which there were no posts are not displayed.
Now as an initial thought i was thinking
Start with the last day of the current Month
And get the Value of the First day of the current Month
Do a MySQL COUNT Query/mysql_num_rows for posts that were date >= First_Day_Seconds AND date <= Last_Day_Seconds
Display or put the values in an Array
Do another Query to Check if Any more values are found WHERE date < First_Day_Seconds (break if no rows were found)
Now the above is just something on the top of my head. But if you got any ideas to speed this process up please share.
Will say in advance, date needs to be in mktime format
I would suggest using a database "native" time format, but it works with UNIX timestamps as well.
You can simply do:
SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;
Optionally with a WHERE clause limiting the dates to past or future dates. Possibly an ORDER clause thrown in for good measure. That should be pretty much all that's needed, let the database do as much work as possible.
If you need more formatting options, select the dates with "%Y-%m" instead and format them in PHP:
date($myCustomFormat, strtotime("$date-01"));
You can use this query to get years
"SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
now use can use this query to get month of that year
$tday = date("Y", $datetime);
$s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";

Categories