Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to get the date range from the week number

Posted on 2009-12-29
7
Medium Priority
?
1,628 Views
Last Modified: 2013-12-13
I am using a 3rd party php calendar to allow the user to pick a single date. I am then storing the week number for that date in the database (using $theDate=strtotime($testDate); $week=date("W",$theDate);). On a separate page, I would like to do a lookup in the database to get the week number, then from that number display the start and end date for the week (week starting on Sunday). I know how to get a week number from a date, but can someone please show me how I can get the date range from the week number?
Thanks!
0
Comment
Question by:AmyL
  • 6
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26142996
Week numbers have a kind of unique use case in PHP and MySQL.  More data here:
http://us2.php.net/manual/en/function.date.php

See the date('W') value.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26143018
This may require some tinkering but see what you get for different years...
<?php // RAY_dates_from_week_number.php
error_reporting(E_ALL);
echo "<pre>\n";


$week  = date('W', strtotime('July 4, 1976'));
var_dump($week);


$daycount = $week * 7;


$datething = date('c', strtotime("January 1, 1976 + $daycount Days"));
var_dump($datething);

Open in new window

0
 

Author Comment

by:AmyL
ID: 26143090
Maybe I'm misunderstanding the code, but that last example doesn't seem to give me a date range.  I would be expecting something like 7/2/1976 - 7/8/1976, preferably in an array with each date in the range.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26144927
See if this article gives you any good hints, and I will look at getting you a better example with a date range.
http://www.experts-exchange.com/articles/Web_Development/Web_Languages-Standards/PHP/Handling-date-and-time-in-PHP-and-MySQL.html

best regards, ~Ray
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26145011
More to follow, but try running this code snippet and you will see some of the "interesting" ways that PHP "works" with the week number.  Looking at this from 50,000 feet my suggestion might be to store the date of Sunday instead of the week number.  I expect that will make the code more intuitive.  If you're expecting millions of highly active calendar entries, you might want to store the date of Sunday as a UNIX timestamp integer and index the column.
<?php // RAY_dates_from_week_number.php
error_reporting(E_ALL);
echo "<pre>\n";

// SHOW HOW WEEK NUMBERS "WORK" IN PHP

// GET A WEEK NUMBER FROM PHP
$week  = date('W', strtotime('Jan 1, 1976'));
var_dump($week);

// HOW MANY DAYS IS THIS INTO THE YEAR
$daycount = $week * 7;

// LET US SEE WHAT PHP GIVES US BACK FOR A DATE
$datething = date('r', strtotime("January 1, 1976 + $daycount Days"));
var_dump($datething);

// SHOW SOME OTHER "INTERESTING EXAMPLES" OF WEEK NUMBERS
$week  = date('W', strtotime('Dec 31, 1976'));
var_dump($week);
$week  = date('W', strtotime('Dec 31, 1977'));
var_dump($week);
$week  = date('W', strtotime('Dec 31, 1978'));
var_dump($week);
$week  = date('W', strtotime('Dec 31, 1979'));
var_dump($week);
$week  = date('W', strtotime('Dec 31, 1980'));
var_dump($week);

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 26146243
This seems to work.  Maybe if I looked at it again in a new light there would be some better way to generalize or simplify the function, but in any case this is a good first cut.  

Best wishes for the new year, ~Ray
<?php // RAY_dates_from_week_number.php
error_reporting(E_ALL);
echo "<pre>\n";


// SHOW HOW WEEK NUMBERS "WORK" IN PHP - ISO8601 WEEKS START ON MONDAY, UNFORTUNATELY
// See http://us.php.net/manual/en/function.date.php
// MAN PAGE SAYS, "W ISO-8601 week number of year, weeks starting on _MONDAY_ (emphasis added)"



// FUNCTION TO GET A DATE RANGE FROM A WEEK NUMBER
function get_date_range_from_week_number($week, $year=NULL, $return=NULL, $echo=FALSE)
{
    // DEFAULT IS THIS YEAR
    if (empty($year)) $year = date('Y');

    // ELIMINATE BOGUS YEARS
    $year = $year - 1;
    if (!$end_of_last_year = strtotime("$year" . '-12-31')) return FALSE;

    // ELIMINATE BOGUS WEEKS
    if ( ($week < 1) || ($week > 53) ) return FALSE;

    /// GET THE WEEK OFFSET
    $week = $week - 1;

    // ADD THE NUMBER OF WEEKS TO THE END OF LAST YEAR
    $day = date('r', $end_of_last_year);
    $day = date('r', strtotime($day . " + $week Weeks"));

    // IF THIS IS A SUNDAY, USE IT AS THE STARTING DATE
    if (date('w', strtotime($day)) == 0)
    {
        $sunday = $day;
    }
    else
    {
        $sunday = date('r', strtotime($day . ' Last Sunday'));
    }

    // FIND THE NEXT SATURDAY
    $saturday = date('r', strtotime($sunday . ' Next Saturday'));

    // OPTIONALLY VERIFY THE WEEK NUMBER OF THE SATURDAY - THE SUNDAY WOULD BE IN THE PREVIOUS WEEK
    if ($echo)
    {
        echo "\n $sunday - $saturday ";
        echo date('W', strtotime($saturday));
    }

    // CONVERT THE DATETIME STRINGS BACK INTO TO TIMESTAMPS
    $sunday   = strtotime($sunday);
    $saturday = strtotime($saturday);

    // RETURN THE UNIX TIMESTAMPS?
    if ($return === NULL) return array($sunday, $saturday);

    // ELSE RETURN THE FORMATTED DATE
	return array( date("$return", $sunday), date("$return", $saturday) );

} // END FUNCTION get_date_range_from_week_number();




// SOME SAMPLE TEST DATA
var_dump(get_date_range_from_week_number(1));
var_dump(get_date_range_from_week_number(2,    '', 'l, F jS, Y'));
var_dump(get_date_range_from_week_number(52, 2009, 'l, F jS, Y'));
var_dump(get_date_range_from_week_number(53, 2010, 'l, F jS, Y'));

var_dump(get_date_range_from_week_number(53, 2009, NULL, TRUE));
var_dump(get_date_range_from_week_number(53, 2009, 'c', TRUE));
var_dump(get_date_range_from_week_number(53, 2009, 'l, F jS, Y'));



// SHOW SOME "INTERESTING EXAMPLES" OF WEEK NUMBERS
$d = 'Jan  1, 1976'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";
$d = 'Dec 31, 1976'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";
$d = 'Jan  1, 1977'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";
$d = 'Dec 31, 1977'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";
$d = 'Jan  1, 1978'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";
$d = 'Dec 31, 1978'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";
$d = 'Jan  1, 1979'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";
$d = 'Dec 31, 1979'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";
$d = 'Jan  1, 1980'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";
$d = 'Dec 31, 1980'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";
$d = 'Jan  1, 1981'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";
$d = 'Dec 31, 1981'; echo date('r', strtotime($d)); $week  = date('W', strtotime($d)); echo " $week \n";

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26162573
Thanks for the points - it is an interesting question, for sure.  Best, ~Ray
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question