Jump to content

month name from week and year


tkm

Recommended Posts

Hello Mates,

  Need a small help. Can anyone provide me insights or some few lines of code on how can I get month's name from providing the week and year information. Say I want to know the name of the month of 2nd week of year 2008. Any help would be greatly appreciated. Thank you.

Link to comment
Share on other sites

From the comments of the last link, there's an easy solution working at my box (PHP 5.2.2, Apache on WinXP):

 

<?php
$week = 2;
$year = 2008;
$week = str_pad($week, 2, 0, STR_PAD_LEFT);
$month = date('F', strtotime($year . 'W' . $week));
echo $month;
?>

 

This echoes the month on the first day of the specified week (on my box, may be system dependent). So week 1 would output December, since week 1 started in December, 2007 (2007-12-31).

Link to comment
Share on other sites

That's easy to do once you have a Unix timestamp. I'm assuming that you know the number of weeks into the year so to get the month number you will need to divide that by 4 (you may need to round it down by using floor() ). Now you can make a Unix timestamp by using the mktime(hour,minute,second,month,day,year) function and the month number and year (you can fill parameters you don't need like hour, day etc with 0). Then you can easily use the date() function to get the month's name like this:

<?php
date("F", $timestamp);
?>

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.