Rivnat Nasah Rivnat Nasah - 2 years ago 143
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.

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download