1

I have an Array of a week with a timestamps like this 2013-01-07 06:55:34 what I would like to do is split the array up to 7 different arrays by the day. Is that possible in PHP?

How I am creating the array is like this

$sqlCount="SELECT time_stamp FROM `download_log` WHERE WEEK(`time_stamp`) = WEEK(CURRENT_TIMESTAMP) AND YEAR(`time_stamp`) = YEAR(CURRENT_TIMESTAMP)";
$queryCount = mysql_query($sqlCount) or die(mysql_error());


$dates = array();

while(($row =  mysql_fetch_assoc($queryCount))) {
    $dates[] = $row['time_stamp'];
}

And what I would like is 7 different arrays like $monday $tuesday etc. and each of those arrays have the days from the $dates array

2
  • 1
    Can you provide example input and expected output? Commented Jan 8, 2013 at 16:27
  • 1
    yup and pretty easy also with a loop and a if... try to do it and post your code, even if it fails, then we can help depure that code and explain what was wrong, it's the best way to learn ^^ Commented Jan 8, 2013 at 16:27

3 Answers 3

3

This can be easily done with PHP's date() function and a little bit of looping action.

Something along the lines of the following should get you going:

// array to hold all of the dates in
$dates = array('Mon' => array(), 'Tue' => array(), 'Wed' => array(),
               'Thu' => array(), 'Fri' => array(), 'Sat' => array(),
               'Sun' => array());

while(($row =  mysql_fetch_assoc($queryCount))) {
    // get the day of the week for the current element
    $dayOfWeek = date('D', $row['time_stamp']);

    // add the current element to the correct day-entry in the `$dates` array
    $dates[$dayOfWeek][] = $row;
}

This is a sample template that can be adapted to use whatever indexes you'd prefer (abbreviated week names, full weeknames, numeric day-of-week, etc). Review the available values from the date() function and adjust to whatever suits your needs better.

EDIT
I've customized my original/generic for-loop above to fit your while-loop that's reading from the database.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure if I was very clear on what I wanted but I figured it out this is what I am doing

$mon = array();
$tue = array();
$wed = array();
$thur = array();
$fri = array();
$sat = array();
$sun = array();

foreach ($dates as $value) {

if(date('D', strtotime($value)) == 'Mon') {
    $mon[] = $value;
    }
    if(date('D', strtotime($value)) == 'Tue') {
    $tue[] = $value;
    }
    if(date('D', strtotime($value)) == 'Wed') {
    $wed[] = $value;
    }
    if(date('D', strtotime($value)) == 'Thu') {
    $thur[] = $value;
    }
    if(date('D', strtotime($value)) == 'Fri') {
    $fri[] = $value;
    }
    if(date('D', strtotime($value)) == 'Sat') {
    $sat[] = $value;
    }
    if(date('D', strtotime($value)) == 'Sun') {
    $sun[] = $value;
    }

}

Comments

0

Why don't you use the built-in WEEKDAY() function in MySQL?

$sqlCount = "SELECT WEEKDAY(`time_stamp`) AS 'weekday', `time_stamp` ...";

You can then push the dates like

$dates[$row['weekday']][] = $row['time_stamp'];

An alternative is the built-in DATE_FORMAT() function. Following use (of the 2nd parameter) will get you abbreviated weekday names.

$sqlCount = "SELECT DATE_FORMAT(`time_stamp`,'%a') AS 'weekday', `time_stamp` ...";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.