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!
LVL 1
t3chguyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ChloesDadCommented:
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 PaseurCommented:
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.
Ray PaseurCommented:
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

Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Ray PaseurCommented:
See if this helps.
http://iconoun.com/demo/temp_t3chguy.php
<?php // demo/temp_t3chguy.php
/**
 * http://www.experts-exchange.com/questions/28915499/PHP-Non-Iso-Dates.html
 * https://en.wikipedia.org/wiki/ISO_8601
 * http://php.net/manual/en/function.date.php
 */
error_reporting(E_ALL);
echo '<pre>';

Class WeekDate
{
    public $iso_date, $weekday, $iso_week, $vb_week;
    public function __construct($date)
    {
        $time = strtotime($date);
        if (!$time) trigger_error("Fail: $date", E_USER_WARNING);

        // STORE THE ISO VALUES
        $this->iso_date = date('Y-m-d', $time);
        $this->iso_week = date('W',     $time);
        $this->weekday  = date('D',     $time);

        // GET THE YEAR
        $year = date('Y-01-01', $time);

        // VB RULES: START ON FIRST DAY WITH WEEK #1 AND INCREMENT ON EACH FOLLOWING SUNDAY
        $vweek = 1;
        $alpha = 0;
        $omega = date('z', $time);

        // COUNT THE SATURDAYS TO KNOW THE SUNDAYS
        while($alpha <= $omega)
        {
            $ymd = date('Y-m-d', strtotime("$year + $alpha DAYS"));
            if (date('D', strtotime($ymd)) == 'Sat') $vweek++;
            $alpha++;
        }
        // EDGE CASE - ROLL THE COUNT BACK IF THE LAST DAY IS SATURDAY
        if (date('D', strtotime($ymd)) == 'Sat') $vweek--;

        // STORE THE VB WEEK VALUE
        $this->vb_week = str_pad($vweek, 2, '0', STR_PAD_LEFT);
    }
}

// EXAMPLES
$x = new WeekDate('2012-01-30');
print_r($x);

$x = new WeekDate('2012-01-01');
print_r($x);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
t3chguyAuthor Commented:
Perfect solution for a crappy problem to have!  Thank you so much!
Ray PaseurCommented:
Glad to help!  Thanks for the points and thanks for using E-E, ~Ray
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.