Jump to content


Photo

Caculate what wednesday is it of the month


  • Please log in to reply
4 replies to this topic

#1 focus72050

focus72050
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 September 2006 - 07:32 AM

Hi everyone,

I am looking for php code that will

return to me whether it is the first, second third or if exists fourth wednesday for the current month,  and by current month the script should decide on its own what the current month is.

Thank you in advance really appreciate it.



#2 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 04 September 2006 - 08:14 AM

$weekposA = array(1=>"first", "second", "third", "fourth");
$month = date("F"); //get the month (Januaray to December)
$day = date("j"); //get day of the week 1-31
$weekpos = ceil($day/7); //determine whether it's the 1st, 2nd, ...of the week

$theweeknum = $weekposA[$weekpos];
$todayweek = date("l");

//if($todayweek == "Wednesday")
echo "Today is the " . $theweeknum . " " . $todayweek . " of " . $month;

To make it display on Wednesday's only, just uncomment my "if($todayweek == "Wednesday")" out.


#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 04 September 2006 - 12:36 PM

There are always at least four Wednesdays in a month. Sometimes there are five.
Legend has it that reading the manual never killed anyone.
My site

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 04 September 2006 - 02:03 PM

to rework extrovertive's response slightly, all you'd have to do to find out which occurrence of the day of the week a certain date is, just run a function like this where you can pass the date to it. then, you've got a very flexible solution that you can use the results in a variety of ways:
<?php
function getWhichWeek($date = '') {
  $date = empty($date) ? date('Y-m-d') : $date; // default to today
  list($year, $month, $day) = explode('-', $date);
  $res = array(
    'day'  => date('l', strtotime($date)),
    'week' => ceil($day/7)
  );

  switch($res['week']) {
    case 1:
      $res['post'] = 'st';
      break;

    case 2:
      $res['post'] = 'nd';
      break;

    case 3:
      $res['post'] = 'rd';
      break;

    default:
      $res['post'] = 'th';
  }

  return $res;
}

$today = getWhichWeek();
echo "Today is the {$today['week']}{$today['post']} {$today['day']} of the month!";
?>

have fun with that!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 focus72050

focus72050
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 05 September 2006 - 04:51 AM

Everyone thank you a million, I really appreciate it.  And yes you are correct there are at least 4 of each day in a month, dont know what I was thinking.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users