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
https://forums.phpfreaks.com/topic/86334-strtotime-help/
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
https://forums.phpfreaks.com/topic/86334-strtotime-help/#findComment-441136
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
https://forums.phpfreaks.com/topic/86334-strtotime-help/#findComment-441144
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
https://forums.phpfreaks.com/topic/86334-strtotime-help/#findComment-441157
Share on other sites

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.