Jump to content

month name from week and year


tkm

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.