Jump to content

Working out montly averages


mtgriffiths

Recommended Posts

Hi all,

 

I am trying to work out the monthly attendance for a student. I have the dates the have logged into the system saved in a database as dd/mm/yyyy.

 

I am not sure how to work out how many days are in each month so that when the total amount of days a month a student has logged in can be worked out a calculation for the average can be found.

 

Any one have any ideas?

 

Thanks in advance

 

Matthew

Link to comment
https://forums.phpfreaks.com/topic/101578-working-out-montly-averages/
Share on other sites

My advice is to convert over to a datetime type column... it's easy enough to convert over, jsut create a script to do it for you

 

Grab all data from sql, save to text file in case stuff breaks, convert row by row dd/mm/yyyy to yyyy-mm-dd, build a new query set, empty table, alter table (change col to datetime type), insert new data :)

 

You can now use my query example.

You can use: cal_days_in_month() do get the number of days in a particular month.

<?php
$num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
echo "There was $num days in August 2003";
?>

 

I am assuming you already know how to do the SQL command; if you don't know how to write the SQL command, then reply.

 

See:

http://us.php.net/cal_days_in_month

 

julian calander functions would prolly be more useful here.

 

but if ya dun have access to those functions (add on to php)

 

u can either build yer own julian calander functions

or play with strtotime

<?php
  header('Content-type: text/plain');
  
  function daysinmonth($date)
  {
  $d1=strtotime($date);
  echo date("Y-m-d",$d1)."\n";
  $d2=strtotime(date("Y-m-01",$d1)); // First day of Month
  echo date("Y-m-d",$d2)."\n";
  $d3=strtotime("+1 Month -1 Day",$d2);
  echo date("Y-m-d",$d3)."\n";
  return intval(date("d",$d3));
  }

  echo daysinmonth("2007-12-31")
  
?>

Don't think they are trying to find if the day is in the month, just trying to find the total days in the month.  Seems to be a pretty simple question.

 

To pull the data from the database, could just use wildcards for the dates.  For example:

 

SELECT * FROM `login` WHERE `date` LIKE '%%%%-01-%%';

 

To count the rows, just use the COUNT()

 

SELECT COUNT(*) FROM `login` WHERE `date` LIKE '%%%%-01-%%';

 

Hi all,

 

I am trying to work out the monthly attendance for a student. I have the dates the have logged into the system saved in a database as dd/mm/yyyy.

 

I am not sure how to work out how many days are in each month so that when the total amount of days a month a student has logged in can be worked out a calculation for the average can be found.

 

Any one have any ideas?

 

Thanks in advance

 

Matthew

 

It is a SQL question, has a database with the login times in it.  "not sure how to work out how many days are in each month" is the question, and that is solved by cal_days_in_month() function.

 

Then needs to count the number of rows with that date which is the SQL statement.

 

Take the SQL result and divided by the value of the cal_days_in_month().  Not that difficult, just need to know how to make the SQL statement and how to use the cal_days_in_month() function.

 

Also stated it was a MySQL database with the login times.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.