Avatar of t3chguy
Flag for United States of America asked on

PHP Non Iso Dates

Extremely frustrated and out of my control problem, as the system I'm integrating with was written by another consultant, but he stores a week number / year number in a database field.

For example 02/16.

Unfortunately, the guy doesn't know anything about date standards and starts 1/1/2015 as week 1, even though it's only a three day week, then 1/4/2015 begins week 2.

However, users use my program, date("w", strtotime("2016-01-04")) returns week number 1, wherein, his system written in VB returns week 2.

Is there a way in PHP to match his dumb logic?

Thanks in advance!

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon

IIRC Week is the week of the 1st Jan if there are 4 or more days in that week, so you could check for the day of the week that the 1st January falls and if that is Mon, Tue, Wed or Thu then he is right, if not add 1 to the value returned by the php call. You will also need to do some checking if this returns a number > 52 and set it back to 1 if it does. There will also be some occasions when Week 53 is valid so they will need to be checked for as well.
Ray Paseur

ISO week numbers are kind of programmatically awkward, to say the least.  At the edge cases, they are not meaningful without knowing the year.  What does week 53 mean in a 52-week year?  Seems confusing!

It sounds like the VB calendar week number starts at 1 and increments on the following Sunday, right?  Here is a visual link to help confirm.

If that's the case, you're not too far off - the ISO week number increments on Monday.  Refs:

I'll tinker with it a little and see if there is an easy programmatic way to resolve the variance.
Ray Paseur

Looks like the ISO week number can be computed by PHP date(), but the only way to know the VB week number is to count the Sundays of the year, with some minor rules changes.  In a lot of years, you can just say that the VB week number equals the ISO week number, except on Sunday when the VB week number is one greater than the ISO week number.  But that rule breaks down in 2010, 2011, 2016 and other years.  These years appear to be characterized by an initial ISO week number of 53 (from the previous year).  I have not run this 100% to ground, so if I'm missing something, please let me know.

Just to be clear, I am understanding this to be a correct representation:
2017-01-01 Sun ISO=52 VB=1 *
2017-01-02 Mon ISO=01 VB=1
2017-01-03 Tue ISO=01 VB=1
2017-01-04 Wed ISO=01 VB=1
2017-01-05 Thu ISO=01 VB=1
2017-01-06 Fri ISO=01 VB=1
2017-01-07 Sat ISO=01 VB=1
2017-01-08 Sun ISO=01 VB=2 *
2017-01-09 Mon ISO=02 VB=2
2017-01-10 Tue ISO=02 VB=2

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Ray Paseur

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

Perfect solution for a crappy problem to have!  Thank you so much!
Ray Paseur

Glad to help!  Thanks for the points and thanks for using E-E, ~Ray