Jump to content

[SOLVED] first date of a known week


golles

Recommended Posts

Hey guys,

 

I have some experience with PHP, but this is something I can't figure out, not even with google...

 

I want to get the date of the monday of a know week and year

 

so for example 20 and 2008 would return 5-12-2008, 19 and 2008 would return 5-5-2008

 

Hopefully someone can help me ;)

 

kind regards,

 

golles

Link to comment
Share on other sites

Something like this should work:

 

<?php
$weekno='19';
$year='2008';
echo date('d-m-y',strtotime('last monday',strtotime('01/01/'.$year)+$weekno*60*60*24*7+60*60*24));

 

Basically, you get the time of the beginning of the year, then add on the number of seconds in the number of weeks required. You then add on one more day, otherwise a year which started on a monday would give odd results. Finally, you format it how you want it.

 

If you're not quite sure what i mean by needing to add an extra day, then think about running this today:

 

<?php
echo date('d-m-y',strtotime('last monday'));
?>

 

This would give the monday of last week's date - when in fact we would want today.

 

Oh, and there is also the question of what you consider to be the first week of the year. For example, 2008 began on a Tuesday - so is the first week of the year beginning of the 7th January, or would you count that as the second week?

 

Link to comment
Share on other sites

try

<?php
$y = 2008;
$w = 20;
$dw = ($w-1)*7;              // days to week w

$wk20 = strtotime("+$dw days", mktime(0,0,0,1,4,$y));      // date in week 20 (wk 1 is week containing jan 4)
$dow = (date('w', $wk20) + 6) % 7;                         // day of week converted to monday = 0
$mon = date('m-d-Y', strtotime("-$dow days", $wk20));      // get monday of week containing $wk20

echo $mon;         // 5-12-2008
?>

Link to comment
Share on other sites

try

<?php
$y = 2008;
$w = 20;
$dw = ($w-1)*7;              // days to week w

$wk20 = strtotime("+$dw days", mktime(0,0,0,1,4,$y));      // date in week 20 (wk 1 is week containing jan 4)
$dow = (date('w', $wk20) + 6) % 7;                         // day of week converted to monday = 0
$mon = date('m-d-Y', strtotime("-$dow days", $wk20));      // get monday of week containing $wk20

echo $mon;         // 5-12-2008
?>

 

That was what I needed!

thank you!

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.