I am building a package that heavily relies on the current week number of the year as well as the forthcoming 4 or 5 week numbers. I know that sounds kind of confusing but lets say this week amounts to the 51st one of the year. The next 4 week numbers would be:
My Question:
How reliable is PHP's date()
function? The library isn't very well documented and the comments underneath make me a little nervous about using it. I am using the following to get the current week number:
echo $weekNumber = date("W");
Is that a reliable way of working with dates? Any recommendations? I am not very good with dates and times and the sheer size of the various functions available in PHP's native library has left me very confused (time()
, strtotime()
, date()
etc).
I've just done a quick test with the following code:
echo date('W', strtotime('2011/12/31')) . '<br>';
echo date('W', strtotime('2011/12/31 +1 week')) . '<br>';
echo date('W', strtotime('2011/12/31 +2 week')) . '<br>';
echo date('W', strtotime('2011/12/31 +3 week')) . '<br>';
Here we start with the 31st Dec 2011 (last day this year), then we print 3 more W
weeks, which gives this output:
52
01
02
03
04
Which does exactly what you want. If PHP's date()
function didn't work properly, it would have either been removed (bad!), or rewritten until it did work.
I would worry more about the reliability of the system clock.