...

View Full Version : Get the week start date and end date when weekno is there



coolumanga
11-11-2011, 05: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, 02: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");
$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.

coolumanga
11-13-2011, 01: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");
$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.

thankz for the reply

when i run this code i will get the error

Fatal error: Class 'DateInterval' not found in D:\Xammp\xampp\htdocs\UU\Untitled-1.php on line 9

so how can i remove this error

plz help

Fou-Lu
11-13-2011, 05:07 PM
thankz for the reply

when i run this code i will get the error

Fatal error: Class 'DateInterval' not found in D:\Xammp\xampp\htdocs\UU\Untitled-1.php on line 9

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum