Jump to content


Photo

I hate mondays


  • Please log in to reply
2 replies to this topic

#1 Marrvel

Marrvel
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationLondon

Posted 11 August 2006 - 04:35 PM

Hi everyone,

Haven't been around here lately but i'm stuck on a problem so I've come crawling back.

I'm trying to write a script that get's all the mondays between 2 dates starting from the monday of the current week,
heres my code so far:

<?php
$today = date('w');

$max_date = "2006-12-31";
$max_day = date('w',strtotime($max_date." 01:01:01"));

/* this gets the start date by going back to the nearest monday from today*/
if($today <> 1) 
{
	$days_from_mon = ($today == 0) ? 6 : $today -1;
	$first_monday = date('Y-m-d',strtotime("-".$days_from_mon." days"));
}
else $first_monday = date('Y-m-d');


/* this gets the last monday by going back to the nearest monday from the max date */
if($max_day  <> 1)
{
	$days_from_mon = ($max_day  == 0) ? 6 : $max_day  -1;
	$last_monday = strtotime($max_date) - (86400 * $days_from_mon);
}
else $last_monday = $max_date;

echo $first_monday." - ".date('Y-m-d',$last_monday)."<br>";

/* this loops though weeks until it meets the max date*/
for($i=0;$week_start <> $last_monday;$i++)
{
	$week_start = strtotime($first_monday) + (604800 * $i); #this adds $i number of weeks to the original week 
	echo date('Y-m-d',$week_start)."<br />"; 
}
?>

This script seems to work fine until it gets to the end of October then for some reason it goes to a sunday which sends it into a infinite loop. I just can't figure out why this happens

Thanks in advance

Rob

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 11 August 2006 - 07:27 PM

try
<?php
$today = (date('w') + 6) % 7;
$max_date = strtotime ('2006-12-31');

$mon = strtotime ("-$today days", mktime(0,0,0));
while ($mon <= $max_date) {
	echo date('D,  d M Y', $mon). '<br />';
	$mon = strtotime ('+7 days', $mon);
}

?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Marrvel

Marrvel
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationLondon

Posted 14 August 2006 - 08:49 AM

Thanks very much Barand, That works a treat.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users