PHP Non Iso Dates

t3chguy
t3chguy used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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.
Most Valuable Expert 2011
Top Expert 2016

Commented:
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.
Most Valuable Expert 2011
Top Expert 2016

Commented:
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

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Most Valuable Expert 2011
Top Expert 2016
Commented:
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

Author

Commented:
Perfect solution for a crappy problem to have!  Thank you so much!
Most Valuable Expert 2011
Top Expert 2016

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial