Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate timestamp from week number

Status
Not open for further replies.

Rekcor

Programmer
Feb 15, 2005
48
0
0
NL
I want to write a function which calculates the timestamp from a week number:

Code:
function timestampFromWeeknr($iWeekNr, $iYear)
{
  //here should come magical calculation

  return $iTimeStamp
}

More accurately speaking, it should be the timestamp from the Monday of that week, at 0:00am.

Has anybody got a clue how I can do this? The other way around (calculate weeknr from timestamp is easy).
 
I found out myself. For those who are interested:

Code:
/**
* PUBLIC getTimestampFromWeeknr($iYear, $iWeek)
* Function to calculate the timestamp of at monday 0:00am
* in given year and week according to ISO-8601 standards
*  
* return: array with TCalenderEvent objects
**/	
function getTimestampFromWeeknr($iYear, $iWeek)
{
  /*
   calculate Monday of first week of year according to ISO-8601 standards
  */

  $iFirstDayOfYear=date("w", mktime (0,0,0,1,1,$iYear));
 
  if ($iFirstDayOfYear>4) //1 jan is on fri, sat or sun, week 1 starts next monday
  {
    $iFirstMondayOfYear=mktime (0,0,0,1,1+(8-$iFirstDayOfYear),$iYear);
  }
  else //1 jan is on mon, tue, wed or thu, week 1 starts on mon
  {
    $iFirstMondayOfYear=mktime (0,0,0,1,1-($iFirstDayOfYear-1),$iYear);
  }	  	  
	  
  /*
   calculate the amount of seconds passed since first week
  */

  $iSecondsPassed=($iWeek-1)*7*24*60*60; 
  
  /*
   return the timestamp of the monday of $iWeek at 00:00am
  */

  return ($iFirstMondayOfYear+$iSecondsPassed);
}
 
Woops! Found a small bug: summer time isnt taken up in my function!

Code:
/**
* PUBLIC getTimestampFromWeeknr($iYear, $iWeek)
* Function to calculate the timestamp of at monday 0:00am
* in given year and week according to ISO-8601 standards
*  
* return: array with TCalenderEvent objects
**/    
function getTimestampFromWeeknr($iYear, $iWeek)
{
  /*
   calculate Monday of first week of year according to ISO-8601 standards
  */

  $iFirstDayOfYear=date("w", mktime (0,0,0,1,1,$iYear));
 
  if ($iFirstDayOfYear>4) //1 jan is on fri, sat or sun, week 1 starts next monday
  {
    $iFirstMondayOfYear=mktime (0,0,0,1,1+(8-$iFirstDayOfYear),$iYear);
  }
  else //1 jan is on mon, tue, wed or thu, week 1 starts on mon
  {
    $iFirstMondayOfYear=mktime (0,0,0,1,1-($iFirstDayOfYear-1),$iYear);
  }            
      
  /*
   calculate the amount of seconds passed since first week
  */

  $iSecondsPassed=($iWeek-1)*7*24*60*60; 
  
  //compensate for summer time
  if (intval(date("H",$iFirstMondayOfYear+$iSecondsPassed))==1) 
  {
    $iSecondsPassed=$iSecondsPassed-3600;
  }

  /*
   return the timestamp of the monday of $iWeek at 00:00am
  */

  return ($iFirstMondayOfYear+$iSecondsPassed);
}
 
Just a thought.. but instead of dealing with seconds and having to worry about specific times.. why don't you just figure out the day number of that monday and use mktime?

This function seems to turn out the right number every time..
Code:
function getTimestampFromWeeknr($iYear, $iWeek)
{
    $firstDayOfYear = date("w",mktime(0,0,0,1,1,$iYear));
    //If Jan 1st is fri, sat, or sun, don't coun't that as a week.
    if($firstDayOfYear<=4) {
        $iWeekNr--;
    }
    $dayNum = $iWeekNr * 7;
    $rawDate = mktime(0,0,0,1,$dayNum,$iYear);
    $rawDateDay = date("w",$rawDate);
    //How far are we from monday?
    $offset = $rawDateDay - 1;
    //Get that monday.
    $newDayNum = $dayNum - $offset;
    $finalDate = mktime(0,0,0,1,$newDayNum,$iYear);
    
    //Check ourselves..
    print "raw date: ".date("Y-m-d D",$rawDate)."<br>";
    print "final date: ".date("Y-m-d D",$finalDate)."<br>";
    print "week number: ".date("W",$finalDate)."<br>";

    return $finalDate;
}

-Dustin
Rom 8:28
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top