How to get a date range if week provided?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gnel2000
Forum Newbie
Posts: 16
Joined: Tue Jul 26, 2005 10:41 pm

How to get a date range if week provided?

Post by gnel2000 »

I was finding a way to get a date range if the week number provided.
Normally, the week(date) function will return a week number if the date provided.
But now i would like to do vice versa. Is anyone have any idea about this?
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post by RobertPaul »

Here's a solution in ColdFusion, if you can manage to translate it...

But I'm pretty stumped. Which is going to bug me...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

use strtotime() to get the timestamp of the first day of the week and add 604800 seconds to the timestamp. (equivilent to one week)

So you have

Code: Select all

//$date is assumed to be a timestamp of the day your searching
if (($date > $timeStart) && ($date < $timeEnd)) {

}
Have a good read of strotime.. it is very useful and extremely flexible
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I was bored and haven't looked at cf in a long time and thought I'd translate the link you sent:

this:

Code: Select all

<cfif IsDefined("form.bleah")>
<cfset firstday = "01/01/"&datepart("yyyy",now())>


<Cfset refday = dateadd("ww", form.checkdate -1 ,firstday)>

<cfset thisday = refday>
<CFSET first_day_of_last_week = DateAdd("d", (DayOfWeek(thisday)-1)*-1, thisday)>
<CFSET last_day_of_last_week = DateAdd("d", 6, first_day_of_last_week)>
</cfif>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
	<title>Untitled</title>
</head>

<body>
<cfif IsDefined("form.bleah")><cfoutput>For week number #form.checkdate#, 
we use #firstday# as a reference (You can manipulate the reference date in line 2 of this script).  #form.checkdate# weeks from this date is
#dateformat(thisday,"mm/dd/yyyy")#, which 
is in the week from #dateformat(first_day_of_last_week,"mm/dd/yyyy")# 
- #dateformat(last_day_of_last_week,"mm/dd/yyyy")# , so #dateformat(first_day_of_last_week,"mm/dd/yyyy")#
is the first day of week #form.checkdate#.


</cfoutput></cfif>
<form action="test.cfm" method="post">
Enter week number:<input type="text" name="checkdate"><br>
<input type="submit" name="bleah" value="Get date info"></form>


</body>
</html>
translates to this:

Code: Select all

<?
if(isset($_POST['bleah']))
{
	$firstday = "01/01/".date("Y");
	$refday = $_POST['checkdate'] -1;
	$refday = date("m/d/Y",strtotime($firstday . " + " . $refday ." weeks"));
	$thisday = $refday;
	$first_day_of_last_week = date("w",strtotime($thisday));
	$forlastweek = 6 - $first_day_of_last_week;
	$first_day_of_last_week = date("m/d/Y",strtotime($thisday . " -" . $first_day_of_last_week . " days"));
	$last_day_of_last_week = date("m/d/Y",strtotime($thisday . " + " . $forlastweek . " days"));
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Untitled</title>
</head>

<body>
<?
if(isset($_POST['bleah']))
{
?>
For week number <?=$_POST['checkdate'];?>, 
we use <?=$firstday;?> as a reference (You can manipulate the reference date in line 2 of this script).  <?=$_POST['checkdate'];?> weeks from this date is
<?=$thisday;?>, which 
is in the week from <?=$first_day_of_last_week;?> 
- <?=$last_day_of_last_week;?> , so <?=$first_day_of_last_week;?> 
is the first day of week <?=$_POST['checkdate'];?>.
<?
}
?>


<form action="test.php" method="post">
Enter week number:<input type="text" name="checkdate"><br>
<input type="submit" name="bleah" value="Get date info"></form>


</body>
</html>
and it works (tested) exactly the same. Not sure if it's what you're after, but the translation is identical....
gnel2000
Forum Newbie
Posts: 16
Joined: Tue Jul 26, 2005 10:41 pm

Post by gnel2000 »

Found this at http://www.php.net, tried, is working

Code: Select all

function findWeekPeriod($week, $year)
{
   $aPeriod = array();

   // if you are a Catholic you should use $x = 0 considering Sunday as the first day of the week. I'm an Orthodox so I consider it the seventh day of the week

   $x = 1;

   $aPeriod['start'] = date("Y-m-d H:i:s", mktime(0, 0, 0, 1, 1 + $week*7 + $x - 6, $year));

   $aPeriod['end'] = date("Y-m-d H:i:s", mktime(0, 0, 0, 1, 1 + $week*7 + $x, $year));

   return $aPeriod;
}
Post Reply