Click to See Complete Forum and Search --> : any suggestions to get "week of" computation?


decibel
07-11-2005, 04:23 AM
Hey guys, I'm looking for suggestions to calculate a "week of" function. Heres what i have below, and what im looking for is a better way to calculate "week of", any suggestions are welcome. Thanks in advance.

// this function gets the weekly label for file names and application display

// $dos is a unix timestamp stored for transaction dates

// currently, the function uses "sunday" to mark the start and end of a week, since transactions only occur monday thru friday, store hours.

function weekof($dos){

if($dos >= '1117324800' && $dos <= '1117929600'){
$weekof = "05 30 05-06 03 05";
}
if($dos >= '1117929600' && $dos <= '1118534400'){
$weekof = "06 06 05-06 10 05";
}
if($dos >= '1118534400' && $dos <= '1119139200'){
$weekof = "06 13 05-06 17 05";
}
if($dos >= '1119139200' && $dos <= '1119744000'){
$weekof = "06 20 05-06 24 05";
}
if($dos >= '1119744000' && $dos <= '1120348800'){
$weekof = "06 27 05-07 01 05";
}
if($dos >= '1120348800' && $dos <= '1120953600'){
$weekof = "07 04 05-07 08 05";
}
if($dos >= '1120953600' && $dos <= '1121558400'){
$weekof = "07 11 05-07 15 05";
}
if($dos >= '1121558400' && $dos <= '1122163200'){
$weekof = "07 18 05-07 22 05";
}
return $weekof;
}

$filename = "transactions-".$weekof.".dat";

NogDog
07-11-2005, 11:15 AM
function weekof($dos)
{
$dayOfWeek = date('w', $dos);
$startOfWeek = $dos - ($dayOfWeek * 60 * 60 *24);
$endOfWeek = $dos + ((6 - $dayOfWeek) * 60 * 60 * 24);
return(sprintf("%s-%s", date('m d y', $startOfWeek), date('m d y', $endOfWeek)));
}

decibel
07-11-2005, 04:42 PM
That looks good. Im not able to test this code yet, as I am away from my developing PC, but I wanted to thank you for the response.

NogDog
07-11-2005, 04:55 PM
That looks good. Im not able to test this code yet, as I am away from my developing PC, but I wanted to thank you for the response.
I tested it on my PC with about 2 weeks of timestamps (doing a for loop with each iteration adding the iteration number * 60 * 60 * 24 to the current time()). Seemed to work fine. :)