...

# Get the week start date and end date when weekno is there

coolumanga
11-11-2011, 06:29 AM
hi Given a weekno i want to find out start date of the week and end date od the week

\$weekno=43

i want to find out start date and the end date of the week.

plz give me a sample code for this

Fou-Lu
11-11-2011, 03:48 PM
This takes some calculations. There is no method to determine the date of a particular year given the week of the year. Fortunately, its very easy to calculate.
In PHP5.3+, you can use the DateTime::add method or strtotime if you are on < 5.3.

\$weekno = 43;
\$oStartYear = new DateTime("January 1"); // Without a year it assumes this year.
if (\$weekno > 1)
{
\$iOffset = \$weekno - 1;
\$oInterval = new DateInterval("P{\$iOffset}W");
}
else
{
\$oOffsetDate = \$oStartYear;
}

print \$oOffsetDate->format('F j Y H:i:s') . '<br />';

// Or using just strtotime:
\$iStart = strtotime('January 1');
if (\$weekno > 1)
{
\$iDays = (\$weekno - 1) * 7;
\$iOffsetDate = strtotime("+{\$iDays} day", \$iStart);
}
else
{
\$iOffsetDate = \$iStart;
}
print date('F j Y H:i:s', \$iOffsetDate);

It can be calculated as well from just numbers as well, but that would take a bit more work then either of the above.

coolumanga
11-13-2011, 02:43 PM
This takes some calculations. There is no method to determine the date of a particular year given the week of the year. Fortunately, its very easy to calculate.
In PHP5.3+, you can use the DateTime::add method or strtotime if you are on < 5.3.

\$weekno = 43;
\$oStartYear = new DateTime("January 1"); // Without a year it assumes this year.
if (\$weekno > 1)
{
\$iOffset = \$weekno - 1;
\$oInterval = new DateInterval("P{\$iOffset}W");
}
else
{
\$oOffsetDate = \$oStartYear;
}

print \$oOffsetDate->format('F j Y H:i:s') . '<br />';

// Or using just strtotime:
\$iStart = strtotime('January 1');
if (\$weekno > 1)
{
\$iDays = (\$weekno - 1) * 7;
\$iOffsetDate = strtotime("+{\$iDays} day", \$iStart);
}
else
{
\$iOffsetDate = \$iStart;
}
print date('F j Y H:i:s', \$iOffsetDate);

It can be calculated as well from just numbers as well, but that would take a bit more work then either of the above.

when i run this code i will get the error

so how can i remove this error

plz help

Fou-Lu
11-13-2011, 06:07 PM

when i run this code i will get the error