Jump to content

[SOLVED] Retrieve Records Of A Week


refiking

Recommended Posts

How can I search for records on a given week?  Say 01/01/08 - 01/08/08

 

Here's the code I have so far:

 

$time = time();
$day = date('Y-m-d');
$out = date('D',$time);
IF ($out == "Mon"){
$wstart = strtotime("$day 00:00:00 -1 day");
$lastweek = strtotime("$day 00:00:00 -8 days");
$twoweeks = strtotime("$day 00:00:00 -15 days");
$threeweeks = strtotime("$day 00:00:00 -23 days");
$fourweeks = strtotime("$day 00:00:00 -30 days");
$fiveweeks = strtotime("$day 00:00:00 -37 days");
$sixweeks = strtotime("$day 00:00:00 -44 days");
}
IF ($out == "Tue"){
$wstart = strtotime("$day 00:00:00 -2 days");
$lastweek = strtotime("$day 00:00:00 -9 days");
$twoweeks = strtotime("$day 00:00:00 -16 days");
$threeweeks = strtotime("$day 00:00:00 -23 days");
$fourweeks = strtotime("$day 00:00:00 -30 days");
$fiveweeks = strtotime("$day 00:00:00 -37 days");
$sixweeks = strtotime("$day 00:00:00 -44 days");
}
IF ($out == "Wed"){
$wstart = strtotime("$day 00:00:00 -3 days");
$lastweek = strtotime("$day 00:00:00 -10 days");
$twoweeks = strtotime("$day 00:00:00 -17 days");
$threeweeks = strtotime("$day 00:00:00 -24 days");
$fourweeks = strtotime("$day 00:00:00 -31 days");
$fiveweeks = strtotime("$day 00:00:00 -38 days");
$sixweeks = strtotime("$day 00:00:00 -45 days");
}
IF ($out == "Thu"){
$wstart = strtotime("$day 00:00:00 -4 days");
$lastweek = strtotime("$day 00:00:00 -11 days");
$twoweeks = strtotime("$day 00:00:00 -18 days");
$threeweeks = strtotime("$day 00:00:00 -25 days");
$fourweeks = strtotime("$day 00:00:00 -32 days");
$fiveweeks = strtotime("$day 00:00:00 -39 days");
$sixweeks = strtotime("$day 00:00:00 -46 days");
}
IF ($out == "Fri"){
$wstart = strtotime("$day 00:00:00 -5 days");
$lastweek = strtotime("$day 00:00:00 -12 days");
$twoweeks = strtotime("$day 00:00:00 -19 days");
$threeweeks = strtotime("$day 00:00:00 -26 days");
$fourweeks = strtotime("$day 00:00:00 -33 days");
$fiveweeks = strtotime("$day 00:00:00 -40 days");
$sixweeks = strtotime("$day 00:00:00 -47 days");
}
IF ($out == "Sat"){
$wstart = strtotime("$day 00:00:00 -6 days");
$lastweek = strtotime("$day 00:00:00 -13 days");
$twoweeks = strtotime("$day 00:00:00 -20 days");
$threeweeks = strtotime("$day 00:00:00 -27 days");
$fourweeks = strtotime("$day 00:00:00 -34 days");
$fiveweeks = strtotime("$day 00:00:00 -41 days");
$sixweeks = strtotime("$day 00:00:00 -48 days");
}
IF ($out == "Sun"){
$wstart = strtotime("$day 00:00:00");
$lastweek = strtotime("$day 00:00:00 -7 days");
$twoweeks = strtotime("$day 00:00:00 -14 days");
$threeweeks = strtotime("$day 00:00:00 -21 days");
$fourweeks = strtotime("$day 00:00:00 -28 days");
$fiveweeks = strtotime("$day 00:00:00 -35 days");
$sixweeks = strtotime("$day 00:00:00 -42 days");
}
$dw1 = date("Y-m-d H:i:s", $wstart);
$dw2 = date("Y-m-d H:i:s", $lastweek);
$dw3 = date("Y-m-d H:i:s", $twoweeks);
$dw4 = date("Y-m-d H:i:s", $threeweeks);
$dw5 = date("Y-m-d H:i:s", $fourweeks);
$dw6 = date("Y-m-d H:i:s", $fiveweeks);
$dw7 = date("Y-m-d H:i:s", $sixweeks);
$w1sql = mysql_query("SELECT * FROM `sitetrac` WHERE time > '$dw1'");
$w1num = mysql_num_rows($w1sql);
Echo $w1num;

Link to comment
https://forums.phpfreaks.com/topic/125534-solved-retrieve-records-of-a-week/
Share on other sites

to calculate the dates

 

<?php
function week_start_end($date, &$Sun, &$Sat)
{
    $dow = date ('w', strtotime ($date));          // day of week
    $start = strtotime ("-$dow days");             // calculate start and end
    $end = strtotime ('+6 days', $start);
    $Sun = date ('Y-m-d', $start);                 // convert to dates
    $Sat = date ('Y-m-d', $end);
}

$today = date('Y-m-d');
week_start_end($today, $sunday, $saturday);

echo "$sunday - $saturday";                        // -->  2008-09-21 - 2008-09-27

?>

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.