Average of dates per months to Graph, how? - php

I working on a website codeigniter based. In my database i have a lot of dates in this format:
yyyy-mm-dd hh:mm:ss
What i want is to represent in a graph, all the 12 months, and for each month the total of dates that i have on my database. For now just a graph for 2012, next year ill change it for 2013.
How can i do it? Any ideas?
I searched a lot for javascript graph/tables such as in google api but none of them gave me everything i needed. Also, how can i read from database the dates and "put them" in each month.
Thanks

At first extract your average dates from Database with for example this code:
SELECT ROUND(AVG(UNIX_TIMESTAMP(`date`)),0) as avg_time
FROM `data`
WHERE `id`>0
GROUP BY Month(`date`)
This SQL works fine and returns average unix_timestamp rounded to the nearest and grouped in Months. In PHP code u can easily cover back to datetime format if needed.
Also u can add in WHERE closure check for year. For example: WHERE YEAR(date)=2012.
Good luck :)

Related

How can I query certain trading days using YQL?

I'm using PHP to build queries in YQL and the Yahoo! Finance API and retrieve historical stock prices. I'm looking for trends over the last 5 trading days (NOT calendar days). The syntax looks like this:
select * from yahoo.finance.historicaldata where symbol = "YHOO" and startDate = "yyyy-mm-dd" and endDate = "yyyy-mm-dd"
This works fine, but the dates are calendar dates, ie if the start date is the Friday before a three-day weekend, I don't get much useful data. Is there:
Some form of expression I can use in the query to specify this (Google Sheets, for instance, lets you subtract a number of days from a given date, and assumes those dates are trading days), or
Some external API or calendar I can use to retrieve and insert the correct start date explicitly?
If this is a case of RTM, forgive me, but please link me to TFM, because I can't seem to find it.

MySQL select specific year or year month rows that have yyyy-mm-dd as the date

Ok, Im sure this is simple and its just evading me at the moment. But Im searching and I can't find it either, maybe I don't know what Im searching for term wise. Anyway.
I have a table, with rows of posts, and within those rows is a column thats in YYYY-MM-DD format, might be YYYY-MM-DD HH:MM:SS either way. I'm Trying to build a query that will let me select either just the entries in a specific year, or specific month of a given year. But since the formats are what they are, Im confusing myself, I know theres a way to format the dates in the query so they will be easy to compare against. But I cant think of it.
My thought process is seriously flawed currently. at the moment on how to handle that.
Note I am using Codeigniter and its ORM as well
You could try this code
FROM_UNIXTIME(datetime_column, '%Y') AS year
or to fetch year specific record
YEAR(datetime_column)='2012'
to fetch month specific record
MONTH(datetime_column)='11'

PHP comparing non A.D. dates

I need to compare Bikram Sambat dates in PHP. Lets say today in AD is 2012-08-03, the BS date today is 2069-04-19. In BS, we have total number of days in a month ranging from 29 to 32. Again, the days are not consistent in every year. Example if this year current month have 31 days, next year same month might have 30 or even 32 days.
The way i could think is, using a php date converter class (which I already have), convert given BS Dates to equivalent AD Dates and compare them. Just i want to know is there any other better way to do this without using the class i have??
By saying comparing here I mean
a) Finding days between the given date ranges.
b) Checking whether a date lies on a given two date ranges
c) And other comparisons we could do with AD dates using php Date Function
thanks any help is highly appreciated.
Generally, I would suggest converting both dates to unix timestamps (seconds since 1970-1-1 AD) which is a simple integer value for comparison.
Apparently, there is no simple logic behind BS so you will not be able to find an analytic expression that directly gets you from here to there. If you do not wish to use an external class you could write some sort of (recursive) function that starts from 1970-1-1 AD which you manually convert to BS and counts up the timestamp from there following the rules for BS.
Have a look at the source code of KashmirStamps.ca/Samvat.html
It is converting BS Dates to AD dates. You will find Javascript Code & Logic. You can easily adapt the logic to PHP code.

PHP/MySQL - Display DateTime as Date

I'm a little knew to SQL & PHP and have been given the task of displaying some information from the database. I know how to query my database and display the info into tables on screen using PHP and so forth; however this time I've been given a slightly different challenge.
I have information stored in the DateTime format in the SQL database and whilst retrieving it I need to strip the time and display only the date. I've had a read through many of the date/time functions for SQL but for some reason this seems to be going almost straight over my head. I've had a browse of a few sites including the two links below, but I'm having a hard time understanding how to do such things within PHP etc. If someone could steer me in the right direction that would be excellent!
2 somewhat related threads I've browsed:
http://www.gfxvoid.com/forums/showthread.php?28576-PHP-Time-Date-Display
http://blogs.x2line.com/al/archive/2006/02/17/1458.aspx
Logically, am I supposed to query the DateTime and then use PHP to reformat it the way I wish to display it? Or am I supposed to format the datetime using an SQL query?
Thanks very much!
Just use the DATE function around the date time.
SQL Column: created_at
2012-05-09 13:46:25
SELECT DATE_FORMAT(DATE(created_at), '%D %M %Y') FROM Table
Returns:
9th May 2012
EDIT as per comment:
To use it in PHP, you can do something like the following:
Query: (Notice the AS clean_date)
SELECT DATE_FORMAT(DATE(created_at), '%D %M %Y') AS clean_date FROM Table
then in in php:
<?php
echo "<tr><td>{$row['clean_date']}</td>";
?>
Can you check this in PHP when Display the date from Mysql
date('Y/m/d', strtotime($datetodisplay));
Sometime when you fetch the date from mysql, we have change that to time by using strtotime() funciton

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