Question: I am trying to compress a log file directory on my mac. The log directory exists.

For example /var/logs/my\_log\_dir/

I am trying to run something like:

```
$ tar –cpzf x.tar.gz /var/logs/my_log_dir

```

I also tried other variations such as compressing only one of the files in the log dir - no luck. All I get from my mac is:

```
Usage:
  List:    tar -tf <archive-filename>
  Extract: tar -xf <archive-filename>
  Create:  tar -cf <archive-filename> [filenames...]
  Help:    tar --help

```

Thoughts?

Answer: 
This function returns the integer week number of the current month. Weeks always start on Monday and counting always starts with 1.

```
function weekOfmonth(DateTime $date)
{
  $dayFirstMonday = date_create('first monday of '.$date->format('F Y'))->format('j');
  return (int)(($date->format('j') - $dayFirstMonday +7)/7) + ($dayFirstMonday == 1 ? 0 : 1);
}

```

Example of use

```
echo weekOfmonth(new DateTime("2020-04-12"));  //2

```

A test for all days from 1900-2038 with the accepted solution from @Anders as a reference:

```
//reference functions
//integer $date (Timestamp) 
function weekOfMonthAnders($date) {
    //Get the first day of the month.
    $firstOfMonth = strtotime(date("Y-m-01", $date));
    //Apply above formula.
    return weekOfYear($date) - weekOfYear($firstOfMonth) + 1;
}

function weekOfYear($date) {
    $weekOfYear = intval(date("W", $date));
    if (date('n', $date) == "1" && $weekOfYear > 51) {
        // It's the last week of the previos year.
        return 0;
    }
    else if (date('n', $date) == "12" && $weekOfYear == 1) {
        // It's the first week of the next year.
        return 53;
    }
    else {
        // It's a "normal" week.
        return $weekOfYear;
    }
}

//this function
function weekOfmonth(DateTime $date)
{
  $dayFirstMonday = date_create('first monday of '.$date->format('F Y'))->format('j');
  return (int)(($date->format('j') - $dayFirstMonday +7)/7) + ($dayFirstMonday == 1 ? 0 : 1);
}

$dt = date_create('1900-01-01');
$end = date_create('2038-01-02');
$countOk = 0;
$countError = 0;
for(;$dt < $end; $dt->modify('+1 Day')){
    $ts = $dt->getTimestamp();
    if(weekOfmonth($dt) === weekOfMonthAnders($ts)){
       ++$countOk; 
    }
    else {
       ++$countError;
    }
}
echo $countOk.' compare ok, '.$countError.' errors';

```

Result: 50405 compare ok, 0 errors
```
//It's easy, no need to use php function
//Let's say your date is 2017-07-02

$Date = explode("-","2017-07-02");
$DateNo = $Date[2];
$WeekNo = $DateNo / 7; // devide it with 7
if(is_float($WeekNo) == true)
{
   $WeekNo = ceil($WeekNo); //So answer will be 1
}  

//If value is not float then ,you got your answer directly

```