The Issue:
We have some products which use a "YYWW" (date('yW')
) format to identify date of creation.
Sometimes we don't even get the week .. all we have to work with is the year value.
I am currently using DateTime->setISODate()
to get dates out of the year and week values.
Since the turn of a new year usually falls in the middle of a calendar week, our minimum-date is typically in the prior year when considering week-One.
This isn't much of a problem in and of itself, bet when trying to convert back to the original date code string, I'm getting the wrong year value.
Goal:
Our goal is to
- store a given YYWW value to the DB as two values, their earliest and latest possible Dates.
- then, from those min/max dates, accurately rebuild the original year/week string when needed for views.
Examples: 'Code' to 'date': OK
- Code "09" becomes min 2008-12-29 / max 2009-12-27
- Code "0901" becomes min 2008-12-29 / max 2009-01-04
- Code "0922" becomes min 2009-05-25 / max 2009-05-31
Examples: 'date' to 'Code': FAILED
- Date 2009-12-27 = "0952" ... success
- Date 2009-05-31 = "0922" ... success
- Date 2009-05-25 = "0922" ... success
- Date 2009-01-04 = "0901" ... success
- Date 2008-12-29 = "0801" ... FAIL: expected "0901"
Tried:
### Simplified for brevity ###
$dateOfMfg = '0901';
$dc = aCustomValidatorAndStuff($dateOfMfg); // returns array('year' => 2009, 'week' => 1)
$dateTimeMin = new DateTime();
$dateTimeMax = new DateTime();
$result = array(
'min' => $dateTimeMin->setISODate($dc['year'], $dc['week'], 1),
'max' => $dateTimeMax->setISODate($dc['year'], $dc['week'], 7),
);
echo '<pre>Result:<br />',var_dump($result),'</pre>';
echo 'Min: ',$result['min']->format('yW'),'<br />'; // "0801" ... Bad
echo 'Min: ',$result['max']->format('yW'),'<br />'; // "0901" ... Good
Summary:
Is there a native way to get the appropriate year value for that "min" year?
What other suggestions come to mind?