php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #11555 date() format addition
Submitted: 2001-06-19 05:41 UTC Modified: 2001-06-20 14:30 UTC
From: martin at humany dot com Assigned: cmv (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.5 OS: any
Private report: No CVE-ID: None
 [2001-06-19 05:41 UTC] martin at humany dot com
i live in sweden, where displaying weeknumbers in calendars of any sort is common. the following code does the work, tho i'd be very happy if it could be supported to be returned from the date() function.

the algorithm is (mondaynumber+7)/7
where mondaynumber is the day of the year of the monday of the current year

switch(date("w")) {
	case 1: $daysub=0; break;
	case 2: $daysub=1; break;
	case 3: $daysub=2; break;
	case 4: $daysub=3; break;
	case 5: $daysub=4; break;
	case 6: $daysub=5; break;
	case 0: $daysub=6; break;
}
$weeknumber=(date ("z", mktime(0,0,0, date("m"), date("d")-$daysub, date("Y")) )+7)/7;

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-06-20 04:44 UTC] martin at humany dot com
i live in sweden, where displaying weeknumbers in calendars of any sort is common. the
following code does the work, tho i'd be very happy if it could be supported to be returned
from the date() function.

switch(date("w",mktime(0,0,0,$mon,$daycnt+1,$year))) {
	case 1: $daysub=0; break;
	case 2: $daysub=1; break;
	case 3: $daysub=2; break;
	case 4: $daysub=3; break;
	case 5: $daysub=4; break;
	case 6: $daysub=5; break;
	case 0: $daysub=6; break;
}
$weeknumber=round((date("z", mktime(0,0,0, date("m"),date("d")+1-$daysub,date("Y")))+7)/7);
if($weeknumber==53) {
	//some years have 53 weeks & some 52
	//so we check next weeks weeknumber, if its 1 this is week 53, otherwise, this is week 1.
	switch(date("w",mktime(0,0,0,date("m"),date("d")+8,date("Y")))) {
		case 1: $daysub=0; break;
		case 2: $daysub=1; break;
		case 3: $daysub=2; break;
		case 4: $daysub=3; break;
		case 5: $daysub=4; break;
		case 6: $daysub=5; break;
		case 0: $daysub=6; break;
	}
	$tmpweeknumber=round( (date("z", mktime(0,0,0, date("m"), date("d")+8-$daysub, date("Y")))+7)/7);
	if($tmpweeknumber==1) $weeknumber=53;
	else $weeknumber=1;
}

 [2001-06-20 08:52 UTC] derick@php.net
You can use the strftime function already, it's the %W modifier.
But it would be useful if date would support it indeed.
 [2001-06-20 08:53 UTC] derick@php.net
Not closed...
 [2001-06-20 11:56 UTC] cmv@php.net
Trouble is (I suppose), some week-of-year functions consider Monday the first day of the week, and some consider Sunday.

I guess two switches in date() should be done.  I'll work on this.
 [2001-06-20 14:30 UTC] cmv@php.net
date('W') has been added to CVS.

It returns the ISO 8601 week number (0-53).  Basically, Monday is the first day of the week, and week #1 of the year is the week that contains January 4.

- Colin
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 02:02:52 2024 UTC