Jump to content

strtotime help


deiffert

Recommended Posts

I'm trying to get the week of the month for 2 weeks ago

 

I am able to get the current week of the month (1 through 4 or 5) like this:

 

$weekNum = date("W") - date("W",strtotime(date("Y-m-01"))) + 1;

 

and I know I can get info for 2 weeks ago like this:

 

strtotime('2 weeks ago')

 

but I can't figure out how to combine them.

 

Any ideas?

Link to comment
Share on other sites

Personally i wouldn't bother using strtotime() - i'd just take away the number of seconds in two weeks from the current time:

 

<?php
$weekNum = date("W",time()-60*60*24*14);
echo $weekNum;
?>

 

Also, you don't need all that to get the current week number. This would suffice:

 

<?php
$weekNum = date("W");
?>

 

Edit: misread this - didn't realise you were asking for the week of the month

Link to comment
Share on other sites

Thanks for the response.

 

It seems like that's giving me the week number (1-52) whereas I'm looking for the week of the month (1-4 or 5)

 

So it gets tricky because if I want the week of the month for 2 weeks ago, I can't just subtract 2 because I need it to go into the previous month (if 2 weeks ago was in the previous month).

 

Does that make any sense?

Link to comment
Share on other sites

Ok, well this would do it based on a date:

 

<?php
$d = '29';
$m = '01';
$y = '2008';
$diff = strtotime("$m/$d/$y") - strtotime("$m/01/$y");
$weeks_diff = floor($diff/(60*60*24*7))+1;//rounding down and adding 1 to allow for the date being the 1st of the month
//rounding up would return 0
echo $weeks_diff;
?>

 

So we just need to use date() to generate the date:

 

<?php
$d = date('d',time()-60*60*24*14);
$m = date('m',time()-60*60*24*14);
$y = date('Y',time()-60*60*24*14);
$diff = strtotime("$m/$d/$y") - strtotime("$m/01/$y");
$weeks_diff = floor($diff/(60*60*24*7))+1;
echo $weeks_diff;
?>

 

 

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.