Jump to content

php Date help needed


jamez141

Recommended Posts

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. ;)

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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 :P )

is there a way to just automate this without me going through every month and typing it in?

 

ta,

James. ;)

Link to comment
Share on other sites

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);
  }
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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! :D

J.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.