Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    122
    Thanks
    6
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    122
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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.
    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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by coolumanga View Post
    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.


  •  

    Posting Permissions

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