Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Week Numbers between two dates in php

Tags:

php

I want to get the week numbers for given two dates i.e from 2012-01-01 to 2012-12-31.The week numbers should fall exactly in the range as specified above.Can u please give suggestions for doing this.

like image 952
user2286462 Avatar asked Aug 29 '26 09:08

user2286462


1 Answers

Something like this should work fine:

<?php
    $startDateUnix = strtotime('2012-01-01');
    $endDateUnix = strtotime('2013-01-01');

    $currentDateUnix = $startDateUnix;

    $weekNumbers = array();
    while ($currentDateUnix < $endDateUnix) {
        $weekNumbers[] = date('W', $currentDateUnix);
        $currentDateUnix = strtotime('+1 week', $currentDateUnix);
    }

    print_r($weekNumbers);
?>

DEMO.

Output:

Array
(
    [0] => 52
    [1] => 01
    [2] => 02
    .........
    [51] => 51
    [52] => 52
)
like image 179
h2ooooooo Avatar answered Sep 01 '26 00:09

h2ooooooo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!