mercredi 26 août 2015

Display data from database wherein the date is within this week

I have a code here that fetches the data in the database with a day within this week. For example: if the date today is 2015-08-27 and the date_paid column in the database has a value of 2015-08-26 and 2015-08-25, it will show the data that has either of those dates. Here's the code that I've tried to fetch the date that is within this week:

// set current date
$date = date('Y-m-d');
// parse about any English textual datetime description into a Unix timestamp 
$ts = strtotime($date);
// find the year (ISO-8601 year number) and the current week
$year = date('o', $ts);
$week = date('W', $ts);
// print week for the current date
for($i = 1; $i <= 7; $i++) {
    // timestamp from ISO week date format
    $ts = strtotime($year.'W'.$week.$i);
    //shows the date for 7days
    $week[$i] = date("Y-m-d", $ts) . "<br>";
}

if($week[1] == $date)
{
    $today = $week[1];
}

if($week[2] == $date)
{
    $today = $week[2];
}

if($week[3] == $date)
{
    $today = $week[3];
}

if($week[4] == $date)
{
    $today = $week[4];
}

if($week[5] == $date)
{
    $today = $week[5];
}

if($week[6] == $date)
{
    $today = $week[6];
}

if($week[7] == $date)
{
    $today = $week[7];
}

$transaction = mysqli_query($connection, "SELECT * FROM transaction WHERE date_paid = '$today'");
$counttrans = mysqli_num_rows($transaction);

This code suppose to fetch the data that has the day for that week. But it doesn't work. Anyone can fix this?

Aucun commentaire:

Enregistrer un commentaire