jamez141 Posted January 12, 2009 Share Posted January 12, 2009 Hello, i'm trying to write a script where the next sunday service is displayed on our church website homepage. I've come accross a problem where i can only get php to display a date 2 sundays from now or 3 sundays from now but i want to get the second sunday of the month. - the output needs to just be a simple echo""; - im guessing i need to then compare the 'next sunday' to a list of ifelse statements containing the first sunday I hop you know what i mean. Anyone know how i could do this? thanks! James. Quote Link to comment https://forums.phpfreaks.com/topic/140543-php-date-help-needed/ Share on other sites More sharing options...
rhodesa Posted January 12, 2009 Share Posted January 12, 2009 here are the basics for what you need: <?php $time = mktime(0,0,0,1,1,2009); //Starting point...this for instance is January 1st, 2009 $sunday = strtotime("Sunday",$time); //Get's the following sunday (if the starting point IS a sunday, it will return the same day) print date('r',$sunday); //Print out the date ?> edit: to find the second, third, etc sunday, just adjust the starting point to seven days later: <?php $time = mktime(0,0,0,1,8,2009); //Starting point...one week in (aka we will find the second sunday) $sunday = strtotime("Sunday",$time); print date('r',$sunday); ?> Quote Link to comment https://forums.phpfreaks.com/topic/140543-php-date-help-needed/#findComment-735465 Share on other sites More sharing options...
jamez141 Posted January 12, 2009 Author Share Posted January 12, 2009 Ok, thanks! i think i can make this work. We have the same services every second, forth and fifth sunday and another on first and a different one on the third. (it gets confusing ) is there a way to just automate this without me going through every month and typing it in? ta, James. Quote Link to comment https://forums.phpfreaks.com/topic/140543-php-date-help-needed/#findComment-735541 Share on other sites More sharing options...
rhodesa Posted January 12, 2009 Share Posted January 12, 2009 try this out. it will do it for the current month. depending on how you control which month is shown, you can make that part dynamic too <?php $month = date('m'); $year = date('Y'); $time = strtotime("Sunday",mktime(0,0,0,$month,1,$year)); for($week = 1;date('m',$time) == $month && date('Y',$time) == $year;$week++){ switch($week){ case 2: case 4: case 5: print "This part is for the 2nd, 4th, and 5th weeks. The date here is: ".date('r',$time)."<br>"; break; case 1: print "This part is for the 1st week. The date here is: ".date('r',$time)."<br>"; break; case 3: print "This part is for the 3rd week. The date here is: ".date('r',$time)."<br>"; break; } $time = strtotime('+7 days',$time); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140543-php-date-help-needed/#findComment-735552 Share on other sites More sharing options...
jamez141 Posted January 12, 2009 Author Share Posted January 12, 2009 Hi, thanks for your help so far.. so i have this at the moment... <?php $month = date('m'); $year = date('Y'); $time = strtotime("Sunday",mktime(0,0,0,$month,1,$year)); for($week = 1;date('m',$time) == $month && date('Y',$time) == $year;$week++){ switch($week){ case 2: case 4: case 5: print "This part is for the 2nd, 4th, and 5th weeks. The date here is: ".date('F j, Y',$time)."<br>"; break; case 1: print "This part is for the 1st week. The date here is: ".date('F j, Y',$time)."<br>"; break; case 3: print "This part is for the 3rd week. The date here is: ".date('F j, Y',$time)."<br>"; break; } $time = strtotime('+7 days',$time); } ?> and this produces this... This part is for the 1st week. The date here is: January 4, 2009 This part is for the 2nd, 4th, and 5th weeks. The date here is: January 11, 2009 This part is for the 3rd week. The date here is: January 18, 2009 This part is for the 2nd, 4th, and 5th weeks. The date here is: January 25, 2009 so it's showing me the dates of all the sundays in this month. - can it display the current month where January is? i cant get this to work - and how do you make it only display the next sunday? Thanks! J. Quote Link to comment https://forums.phpfreaks.com/topic/140543-php-date-help-needed/#findComment-735579 Share on other sites More sharing options...
DeanWhitehouse Posted January 12, 2009 Share Posted January 12, 2009 can it display the current month where January is? i cant get this to work Huh? Quote Link to comment https://forums.phpfreaks.com/topic/140543-php-date-help-needed/#findComment-735580 Share on other sites More sharing options...
jamez141 Posted January 12, 2009 Author Share Posted January 12, 2009 oh yeah. urm.. haha woops Quote Link to comment https://forums.phpfreaks.com/topic/140543-php-date-help-needed/#findComment-735584 Share on other sites More sharing options...
DeanWhitehouse Posted January 12, 2009 Share Posted January 12, 2009 If you only want it to display the next sunday you can either limit the loop to 1 or not use a loop and do it manually Quote Link to comment https://forums.phpfreaks.com/topic/140543-php-date-help-needed/#findComment-735586 Share on other sites More sharing options...
rhodesa Posted January 12, 2009 Share Posted January 12, 2009 to display the current month, use the date() function: print date('F'); if you just want the NEXT sunday: $this_sunday = strtotime("Sunday"); $week = ceil(date('d',$this_sunday) / 7); print "This Sunday is on ".date('r',$this_sunday)." and is week $week"; Quote Link to comment https://forums.phpfreaks.com/topic/140543-php-date-help-needed/#findComment-735595 Share on other sites More sharing options...
jamez141 Posted January 12, 2009 Author Share Posted January 12, 2009 Aha! Hopefully this will work... <?php $this_sunday = strtotime("Sunday"); $week = ceil(date('d',$this_sunday) / 7); print "This Sunday is on ".date('r',$this_sunday)." and is week $week"; echo"<br><br>"; if ($week == "1"){print "This sunday there is a Family service starting at 10:30 Click here for more";} elseif ($week == "2"){print "This sunday there is Come Together and Get Together.";} elseif ($week == "3"){print "This Sunday there is Praise Together.";} elseif ($week == "4"){print "This sunday there is Come Together and Get Together.";} elseif ($week == "5"){print "This sunday there is Come Together and Get Together.";} Thanks for you help! J. Quote Link to comment https://forums.phpfreaks.com/topic/140543-php-date-help-needed/#findComment-735638 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.