Hector J. Faudoa
Hector J. Faudoa

Reputation: 13

Get Total Sales by Day in PHP

I've never done this before, so im lost here.

I have the following transactions table:

transaction_id  transaction_user    transaction_ammount transaction_time
1                     2                   950              1318482908
2                     2                   750              1318482916
3                     2                   950              1318482939

Then, I need to create a query, that helps me pull Weekly data, Grouped by Day so I can create a Chart, you get me?

I've got this so far in PHP that detects the week number and prints the complete Week Days (Im using phpbb3):

    $format = 'l'; 
    $ts = time();   // set current Unix timestamp
    $today = date($format, $ts); // set today
    $year = date('o', $ts);
    $week = date('W', $ts);
    $weekdays = 7; 


    for($i = 1; $i <= $weekdays; $i++) 
    {
        // timestamp from ISO week date format
        $ts = strtotime($year.'W'.$week.$i);

        $day = date($format, $ts);
        // test if $day is $today
        if ($day == $today) {
            $day = '<strong>' . $day . '</strong>';
        }

        $template->assign_block_vars('time', array(
            'DATE'                  => $day,
        ));
    }

Now, I want to arrange the total sales that happened on Monday, Tuesday, .... etc...

Upvotes: 1

Views: 3075

Answers (1)

bumperbox
bumperbox

Reputation: 10214

this mysql query

select week(transaction_time) as weekno, weekday(transaction_time) as dayno, sum(transaction_ammount) as total_amount
group by     week(transaction_time), weekday(transaction_time)

will give you this result set

|weekno|dayno|total_amount

here are some references to those functions in mysql so you can interpret the data

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekday

UPDATE, try this sql as it might need to convert the timestamps for the other functions to work properly

select week(FROM_UNIXTIME(transaction_time)) as weekno, weekday(FROM_UNIXTIME(transaction_time)) as dayno, sum(transaction_ammount) as total_amount
group by    week(FROM_UNIXTIME(transaction_time)), weekday(FROM_UNIXTIME(transaction_time))

Upvotes: 1

Related Questions