Link to home
Start Free TrialLog in
Avatar of t3chguy
t3chguyFlag 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 ChloesDad
ChloesDad
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
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.
http://lefantasio.com/tag-2015-calendar_%20_%20_%20.html

If that's the case, you're not too far off - the ISO week number increments on Monday.  Refs:
https://en.wikipedia.org/wiki/ISO_8601
http://php.net/manual/en/function.date.php

I'll tinker with it a little and see if there is an easy programmatic way to resolve the variance.
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

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of t3chguy

ASKER

Perfect solution for a crappy problem to have!  Thank you so much!
Glad to help!  Thanks for the points and thanks for using E-E, ~Ray