Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

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

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

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

• 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.
PHP Code:
``` \$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");    \$oOffsetDate = \$oStartYear->add(\$oInterval);}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.

• Originally Posted by Fou-Lu
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.
PHP Code:
``` \$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");     \$oOffsetDate = \$oStartYear->add(\$oInterval); } 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

• Originally Posted by coolumanga

when i run this code i will get the error

so how can i remove this error

plz help
You can't. It means your version of PHP is < 5.3. Use the second block of code with strtotime instead.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•