Jump to content

Tricky Time Function for Reports


jwwceo

Recommended Posts

Hello,

 

My script records order date/time in UNIX timestamp. I want to write some reporting tools to show sales for every month since our store began. So basically, I need to write a function that will find the current month, say October 2008, and then all the previous months BEGINNING and END timestamps, going back to a start date. Then, I can use those BEGINNING and END timestamps in my mysql query to generate the reports.

 

Seems like a tricky endeavor, so I am wondering if there are any php tricks to make this easier.

 

James

Link to comment
Share on other sites

mktime() and date() is what you want....the following will generate start/end timestamps for each month, starting from this month and ending with the month that $begin is in. $begin you should generate by selecting the MIN() timestamp from your table

 

<?php
  $begin = 1170493200; // 2/3/2007 03:00:00
  $min = mktime(0,0,0,date('m',$begin),1,date('Y',$begin));

  print '<table border=1><tr><th>Start Unix</th><th>Start Date</th><th>End Unix</th><th>End Date</th></tr>';
  for(
    $start = mktime(0,0,0,date('m'),1,date('Y')); //This month
    $start >= $min;
    $start = mktime(0,0,0,date('m',$start)-1,1,date('Y',$start)) //subtract a month
  ){
    $end = mktime(0,0,0,date('m',$start)+1,1,date('Y',$start)) - 1; //Next month minus 1 second
    printf('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',$start,date('r',$start),$end,date('r',$end));
  }
  print '</table>';
?>

Link to comment
Share on other sites

The problem with a Unix timestamp is that to do anything beyond comparisons, you must convert it to a usable format.

 

A database can produce most common reports, using sums, counts, min, max, ave... by year/month... This however requires that you EXTRACT the year/month information from the data, GROUP the data by the year/month, and the use the correct aggregate function to give you the results you need.

 

You can use the mysql FROM_UNIXTIME() to give you a DATETIME value from you Unix timestamp. You can use the mysql EXTRACT() function with a YEAR_MONTH parameter to give you year/month values that you can then do a GROUP BY on.

 

For example, if you wanted a sum of total sales by year/month -

 

SELECT EXTRACT(YEAR_MONTH,FROM_UNIXTIME(your_unix_timestamp_column)) as yearmonth,
SUM(sales) AS Total FROM your_table WHERE your_where_conditions_here GROUP BY yearmonth

Link to comment
Share on other sites

I've got this query worked up...but its not working...can anyone see something wrong with this syntax?? Ive never used these extract functions before...might have a typo...

 

Im getting this error..

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM_UNIXTIME(date)) as yearmonth,
SUM(total) AS Total FROM x_cart_orders WHER' at line 1 

 

Query

 

SELECT EXTRACT(YEAR_MONTH FROM_UNIXTIME('date')) as yearmonth,
SUM(total) AS Total FROM x_cart_orders WHERE status = 'C' GROUP BY yearmonth

Link to comment
Share on other sites

Oops. Copy and paste error on my part in the original post. I copied the function name (to avoid spelling mistakes), but did not copy the rest of the syntax (couldn't they be consistent about parameter usage.) . P.S. looking up the function in the manual to see what it wanted there would have helped you solve this yourself.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.