Rivnat Nasah Rivnat Nasah - 5 months ago 9
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
but it is for week of year, but in my case, i need for the month.

Current Result: 6.

Desire Result: 4.


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);