Find The Number Of A Week In A Month For A Given Date


/ Published in: PHP
Save to your folder(s)

This simple function allows you to find a weeks position in the month based on a given date. For instance I needed to alert the user their date was the "Second Week Of January" during a given month. I may never use it again but it came in very handy for this particular project.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. function week_number( $date = 'today' )
  4. {
  5. return ceil( date( 'j', strtotime( $date ) ) / 7 );
  6.  
  7. }
  8.  
  9. ?>
  10.  
  11. Usage Example:
  12.  
  13. <?php
  14.  
  15. $week_num = week_number();
  16.  
  17. $month = date("J", time());
  18.  
  19. echo "Today is the $week_num week of $month";
  20.  
  21. ?>

URL: http://www.aristoworks.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.