]> granicus.if.org Git - php/commitdiff
date('W') now returns week of year (ISO 8601)
authorColin Viebrock <cmv@php.net>
Wed, 20 Jun 2001 18:07:53 +0000 (18:07 +0000)
committerColin Viebrock <cmv@php.net>
Wed, 20 Jun 2001 18:07:53 +0000 (18:07 +0000)
ext/standard/datetime.c

index c0f166c48a4f2570a6bf64337c1ba56d3f49dc04..e94f1a3bc6b883f1cce631ece305e22452c82685 100644 (file)
@@ -218,7 +218,7 @@ php_date(INTERNAL_FUNCTION_PARAMETERS, int gm)
        pval **format, **timestamp;
        time_t the_time;
        struct tm *ta, tmbuf;
-       int i, size = 0, length, h, beat;
+       int i, size = 0, length, h, beat, fd, wd, yd, wk;
        char tmp_buff[32];
 
        switch(ZEND_NUM_ARGS()) {
@@ -299,6 +299,7 @@ php_date(INTERNAL_FUNCTION_PARAMETERS, int gm)
                        case 'a':               /* am/pm */
                        case 'S':               /* standard english suffix for the day of the month (e.g. 3rd, 2nd, etc) */
                        case 't':               /* days in current month */
+                       case 'W':               /* ISO-8601 week number of year, weeks starting on Monday */
                                size += 2;
                                break;
                        case '\\':
@@ -505,6 +506,17 @@ php_date(INTERNAL_FUNCTION_PARAMETERS, int gm)
 #endif
                                strcat(return_value->value.str.val, tmp_buff);
                                break;
+                       case 'W':               /* ISO-8601 week number of year, weeks starting on Monday */
+                               wd = ta->tm_wday==0 ? 7 : ta->tm_wday;
+                               yd = ta->tm_yday + 1;
+                               fd = (7 + (wd - yd) % 7 ) % 7;
+                               wk = ( (yd + fd - 1) / 7 ) + 1;
+                               if (fd>3) {
+                                       wk--;
+                               }
+                               sprintf(tmp_buff, "%d", wk);  /* SAFE */
+                               strcat(return_value->value.str.val, tmp_buff);
+                               break;
 
                        default:
                                length = strlen(return_value->value.str.val);