Rivnat Nasah Rivnat Nasah - 6 months ago 16
PHP Question

PHP - Get week number from date

I have a date, I want to get the week number of that month, how can i do that?

I tried:

$time = '2016/05/21';
echo date("w", strtotime($time));


I also tried with
W
but it is for week of year, but in my case, i need for the month.

Current Result: 6.

Desire Result: 4.

Answer

Try This.

  function weekOfMonth($date) {
    //Get the first day of the month.
    $firstOfMonth = strtotime(date("Y-m-01", $date));
    //Apply above formula.
    return intval(date("W", $date)) - intval(date("W", $firstOfMonth)) + 1;
  }

You can see it here here

You must execute as

$datee = "2016/08/10";
$datee = strtotime( str_replace("/", "-", $datee));
echo weekOfMonth($datee);
Comments