├── .gitignore ├── .travis.yml ├── tests ├── bootstrap.php └── DateGetWeekNumberTest.php ├── phpunit.xml.dist ├── src └── date_get_week_number.php ├── composer.json ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | phpunit.xml 2 | composer.lock 3 | build 4 | vendor 5 | *.phar 6 | .idea 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.2 5 | - 5.3 6 | - 5.4 7 | - 5.5 8 | 9 | script: phpunit 10 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./tests 6 | 7 | 8 | 9 | 10 | ./src 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/date_get_week_number.php: -------------------------------------------------------------------------------- 1 | format('W'); 17 | // if current day of the week is Sunday increment week 18 | if (0 == $datetime->format('w')) { 19 | $week ++; 20 | } 21 | 22 | return str_pad($week, 2, 0, STR_PAD_LEFT); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dv-affection/date_get_week_number", 3 | "description": "Provide support for obtaining a week number of year (week starts on Sunday)", 4 | "type": "library", 5 | "keywords": [ 6 | "php", "date", "week" 7 | ], 8 | "homepage": "https://github.com/dvAffection/date_get_week_number", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "dV", 13 | "email": "dvAffection@gmail.com" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.2.0" 18 | }, 19 | "support": { 20 | "issues": "https://github.com/dvAffection/date_get_week_number/issues", 21 | "source": "https://github.com/dvAffection/date_get_week_number" 22 | }, 23 | "autoload": { 24 | "files": [ 25 | "src/date_get_week_number.php" 26 | ] 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/DateGetWeekNumberTest.php: -------------------------------------------------------------------------------- 1 | setDate(2013, 1, 1); 10 | 11 | $actual = \date_get_week_number($date); 12 | $expected = '01'; 13 | $this->assertSame($expected, $actual); 14 | } 15 | 16 | public function testGeneral() 17 | { 18 | $date = new \DateTime; 19 | $date->setDate(2013, 1, 6); 20 | 21 | $actual = \date_get_week_number($date); 22 | $expected = 2; 23 | $this->assertEquals($expected, $actual); 24 | 25 | 26 | $date = new \DateTime; 27 | $date->setDate(2013, 12, 29); 28 | $actual = \date_get_week_number($date); 29 | $expected = 53; 30 | $this->assertEquals($expected, $actual); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # date_get_week_number() for PHP 2 | 3 | [![Build Status](https://api.travis-ci.org/dVaffection/date_get_week_number.png)](https://travis-ci.org/dVaffection/date_get_week_number) 4 | 5 | According to the [documentation](http://php.net/manual/en/function.date.php) for `date` function of the `W` format character 6 | > ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) 7 | 8 | `date_get_week_number` amends this issue by retuning a week number of a year where the week starts on Sunday 9 | 10 | # Requirements 11 | **PHP >= 5.2.0** because of the `DateTime` object usage 12 | 13 | # Usage 14 | 15 | ```php 16 | $date = new \DateTime; 17 | $week = \date_get_week_number($date); 18 | ``` 19 | 20 | # Installation 21 | 22 | Via composer: 23 | ```javascript 24 | { 25 | "require": { 26 | "dv-affection/date_get_week_number": "~0.1" 27 | } 28 | } 29 | ``` 30 | 31 | # Tests 32 | 33 | ``` 34 | cd date_get_week_number; phpunit; 35 | 36 | ... 37 | OK (2 tests, 3 assertions) 38 | ``` 39 | 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 dV 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | --------------------------------------------------------------------------------