Get week number (in the year) from a date PHP

286,933

Solution 1

Today, using PHP's DateTime objects is better:

<?php
$ddate = "2012-10-18";
$date = new DateTime($ddate);
$week = $date->format("W");
echo "Weeknummer: $week";

It's because in mktime(), it goes like this:

mktime(hour, minute, second, month, day, year);

Hence, your order is wrong.

<?php
$ddate = "2012-10-18";
$duedt = explode("-", $ddate);
$date  = mktime(0, 0, 0, $duedt[1], $duedt[2], $duedt[0]);
$week  = (int)date('W', $date);
echo "Weeknummer: " . $week;
?>

Solution 2

$date_string = "2012-10-18";
echo "Weeknummer: " . date("W", strtotime($date_string));

Solution 3

Use PHP's date function
http://php.net/manual/en/function.date.php

date("W", $yourdate)

Solution 4

This get today date then tell the week number for the week

<?php
 $date=date("W");
 echo $date." Week Number";
 ?>

Solution 5

Just as a suggestion:

<?php echo date("W", strtotime("2012-10-18")); ?>

Might be a little simpler than all that lot.

Other things you could do:

<?php echo date("Weeknumber: W", strtotime("2012-10-18 01:00:00")); ?>
<?php echo date("Weeknumber: W", strtotime($MY_DATE)); ?>
Share:
286,933

Related videos on Youtube

AJFMEDIA
Author by

AJFMEDIA

Updated on December 02, 2021

Comments

  • AJFMEDIA
    AJFMEDIA over 2 years

    I want to take a date and work out its week number.

    So far, I have the following. It is returning 24 when it should be 42.

    <?php
    $ddate = "2012-10-18";
    $duedt = explode("-",$ddate);
    $date = mktime(0, 0, 0, $duedt[2], $duedt[1],$duedt[0]);
    $week = (int)date('W', $date);
    echo "Weeknummer: ".$week;
    ?>
    

    Is it wrong and a coincidence that the digits are reversed? Or am I nearly there?

    • fedmich
      fedmich about 10 years
      Just to add, don't forget to set the timezone, using PHP.ini or via date_default_timezone_set('Asia/Singapore');
    • Grumpy
      Grumpy about 4 years
  • Mike Sherrill 'Cat Recall'
    Mike Sherrill 'Cat Recall' about 12 years
    That's one rule (from ISO 8601, in part). That's what php's date('W', $date) is based on. That's not the only rule, though.
  • Suneth Kalhara
    Suneth Kalhara over 10 years
    i have different year datas, is that possible to use date('W', $date)
  • lukaserat
    lukaserat almost 10 years
    You can use Carbon, "github.com/briannesbitt/Carbon" to maximize your skills in Date manipulation. For carbon you can do that by: Carbon::createFromFormat('Y-m-d H', '2012-10-18')->format('W');
  • Madara's Ghost
    Madara's Ghost almost 10 years
    Actually, you're better off just using PHP's native DateTime objects.
  • PeeHaa
    PeeHaa almost 10 years
    @lukaserat How is that any different from what the accepted answer uses (besides the fact OP has to include an external dependency for something that is already native in the language)?
  • lukaserat
    lukaserat over 9 years
    @PeeHaa In many times I don't want to DRY my codes, if there is already good one.
  • beetstra
    beetstra over 9 years
    I would say that the order in mktime() is wrong, not the OP's order. Of course, the latter is easier to change.
  • Madara's Ghost
    Madara's Ghost over 9 years
    @beetstra I'd say that today, in 2014, if you use mktime() instead of new DateTime, it's most definitely you who are wrong :)
  • Refilon
    Refilon about 7 years
    As fedmich also suggested, use new DateTime($date, new DateTimeZone('Europe/Amsterdam')); for your own timezone :)
  • Farid Abbas
    Farid Abbas over 5 years
    Yesssss, Its working... Perfect calculation. Thanks @Madara Uchiha.
  • Shah Abaz Khan
    Shah Abaz Khan about 5 years
    If the last week of December is also the first week of next year, (like in 2019), then you will get week number as 1, for dates falling in that week (like 30, 31)
  • Loek Bergman
    Loek Bergman about 4 years
    Welcome to stackoverflow.com. Interesting side point that weeknumbers change differently in different calendars. However, please use a DateTime-object for your code. Furthermore you are not declaring variables before using them. It is allowed in PHP, but not considered best practice. Anyhow, thank you for your first contribution.
  • Drea58
    Drea58 over 3 years
    2017-01-01 is a Sunday. Adding a day moves you into the next week, with weeks starting from Monday. 2017-01-01 is the 52th Week of 2016.
  • Drea58
    Drea58 over 3 years
    @Shah Abaz Khan In such cases where you need the year information, you can use strftime(). I've added it as an answer to this question.
  • peterxz
    peterxz almost 3 years
    This doesnt work, you must do date("W", strtotime($yourdate)) instead.
  • S.Walsh
    S.Walsh over 2 years
    This is a neat solution for year and week, though unfortunately strftime() has been deprecated as of PHP 8.1.0 php.net/manual/en/function.strftime.php