Have a problem with counting numbers!

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
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Have a problem with counting numbers!

Post by AshrakTheWhite »

what this code does is give the start and end date of the given week of year

what i need it to do is count FROM the start date TO the end date of the numbers, aka count out the dates in the given week.

any help is appretiated!

Code: Select all

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

    $startDay = 'Mon';
    $endDay = 'Sun';

    $startdate =  strtotime('+' . $week . ' week',mktime(0,0,0,1,1,$year)); 

    $enddate = $startdate;
    while(date("D",$startdate) != $startDay){
       $startdate = mktime(0,0,0,date("m",$startdate),date("d",$startdate)-1, date("Y",$startdate));     
    }
    while(date("D",$enddate) != $endDay){
       $enddate = mktime(0,0,0,date("m",$enddate),date("d",$enddate)+1, date("Y",$enddate));     
    }

    return array('start' => date('l d/m/y', $startdate), 
                 'end'  => date('l d/m/y', $enddate)); 

}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

I read your question a couple of times but still not sure what you are trying to achieve
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I think he wants to be able to pass a week number (1-52) and a year to a function and it should return the start and end dat of that week.

For example, if he passed week 1 and any year it would return 01/01/xx and 07/01/xx.

I'm not sure how to help you, but I'm sure someone will reply with something helpful soon :)
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Post by AshrakTheWhite »

nvm got it working :)

Code: Select all

function findWeekPeriod($week, $year)
{
	$startDay = 'Mon';
	$endDay = 'Sun';

	$startdate =  strtotime('+' . $week . ' week',mktime(0,0,0,1,1,$year)); 

	$enddate = $startdate;
	
	
	
	while(date("D",$startdate) != $startDay){
		$startdate = mktime(0,0,0,date("m",$startdate),date("d",$startdate)-1, date("Y",$startdate));     
	}
	while(date("D",$enddate) != $endDay){
		$enddate = mktime(0,0,0,date("m",$enddate),date("d",$enddate)+1, date("Y",$enddate));     
	}
	
	$days_in_month = cal_days_in_month(CAL_GREGORIAN, date('n', $startdate), date('Y', $startdate));
	
	$startdate = date('j', $startdate);
	$enddate = date('j', $enddate);
	
	$return = array();
	
	
	$i = $startdate;
	while ($i <> $enddate+1)
	{
		$return[] = $i;
		if ($i == $days_in_month)
		{
			$i = 0;
		}
		$i++;
	}
		
		
		
		
	return $return; 
}
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Post by AshrakTheWhite »

hey got yet another problem!

i need this function to return a timestamp instead of what its returning at the moment, i tryed doing it in the while loops but the PC ran out of memory doing it (64MB on server allocated)


heres the code:

Code: Select all

function findWeekPeriod($week, $year)
{	
	$startDay = 'Mon';
	$endDay = 'Sun';

	$startdate =  strtotime('+' . $week . ' week',mktime(0,0,0,1,1,$year)); 

	$enddate = $startdate;
	
	
	
	while(date("D",$startdate) != $startDay){
		$startdate = mktime(0,0,0,date("m",$startdate),date("d",$startdate)-1, date("Y",$startdate));     
	}
	while(date("D",$enddate) != $endDay){
		$enddate = mktime(0,0,0,date("m",$enddate),date("d",$enddate)+1, date("Y",$enddate));     
	}
	
	$days_in_month = cal_days_in_month(CAL_GREGORIAN, date('n', $startdate), date('Y', $startdate));
	
	$startdate = date('j', $startdate);
	$enddate = date('j', $enddate);
	
	$return[0] = '0';
	
		
	$i = $startdate;

	while ($i <> $enddate)
	{
		$return[] = $i;
		if ($i == $days_in_month)
		{
			$i = 0;
		}
		$i + 1;
	}
	
	unset($return[0]);
	
		
	#$return['start'] = $startdate;
	#$return['end'] = $enddate;
	
	return $return; 
}
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

well i dont know exactally how your time is being returned in the array but turn it into a string and use mktime on it http://ee2.php.net/manual/en/function.mktime.php also look at the other functions in the manual about getting a unix time stamp.

a print_r() or the return would be helpful to help you figure out how to make that time a unix time stamp.
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Post by AshrakTheWhite »

well the time being returne is just a date number aka

Code: Select all

0 => 28
1 => 29
3 => 30
4 => 1
5 => 2
6 => 3

what i need is it to return a timestamp for each of those dates
Post Reply