2

I'm trying to accomplish the following task. I'm developing a custom calendar with three views (day,week and month), there may be something out there already but I'm rewriting this as part of learning tool for me as well.

So user will face with Day view when they first visit, with arrows to go back and forth to next day or previous day of course. If they click on Week View, it will give them a 7 days overview with today date as default, and once again they can go back and forth to next week or previous week. The last view is the full month calendar, once they click on the day, it will give them the detail of the day and at the same time reset the default as the day they pick. So if they go back to week view, they will see the detail for the week contain the day that they picked. This is where I have trouble wrapping my head around with, I know there are PHP functions that determine the day of the week but I can't seem to think about how to pass in the date and get the full week starting from Sunday for the day that passed in. For example, if I passed in 10/12/2012, I'd like to start the week at 10/07/12 - 10/13/12.

Thank you kindly for your help or pointing to the right direction. Please excuse my grammar/spelling mistakes as well.

hakre
  • 193,403
  • 52
  • 435
  • 836
derikluv
  • 23
  • 2
  • 1
    `date('N')` gives you the day-of-week, e.g. tuesday = 2. a sunday-saturday range covering that day would be curdate-2 -> curdate+4 – Marc B Oct 12 '12 at 15:42
  • You should really consider doing it this in a javascript framework like jQuery with ajax calls to the server, or a php package like pear offers. It will make your life a lot easier, and believe me, you'll still be learning plenty. – Eric Leroy Oct 12 '12 at 17:37

4 Answers4

2
  1. Assuming that $selection represents the user's selected date as an integer timestamp, date('N', $selection) will return a numeric representation of day of the week of their selection (e.g. Monday = 1 through Sunday = 7).

    This result also represents the number of days since the start of the selected week (the previous Sunday) - $offset. Of course, if you're beginning with a string representation of the users selected date (as in your question, 10/12/2012), you would first need to convert the date to an integer timestamp.

    $selection = strtotime($selection); //if $selection is in string format
    $offset = date('N', $selection);
    


  2. Now you may use $offset to establish date of the the start of the week (the previous Sunday) - $weekstart.

    $weekstart = strtotime("$selection -$offset day");
    


  3. Once you've got the start of the week, the end of the week ($weekend) is, of course, 6 days later, but to calculate this date, you first need to convert $weekstart from an integer timestamp into a string representation of the date. You'll also need to convert the result ($weekend) into a string representation of the date.

    $weekstart = stringftime("%m/%d/%Y", $weekstart); //date format = 10/07/2012
    $weekend = strtotime("$weekstart +6 day");
    $weekend = stringftime("%m/%d/%Y", $weekend);
    



So the following:

$selection = "10/12/2012";
$selection = strtotime($selection);
$offset = date('N', $selection);
$weekstart = strtotime("$selection -$offset day");
$weekstart = stringftime("%m/%d/%Y", $weekstart);
$weekend = strtotime("$weekstart +6 day");
$weekend = stringftime("%m/%d/%Y", $weekend);

$output = "Selected Date = $selection \n Selected Week = $weekstart - $weekend";

echo $output;

results in:

Selected Date = 10/12/2012
Selected Week = 10/07/2012 - 10/13/2020



See:

http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.strftime.php

coderabbi
  • 2,261
  • 16
  • 18
0

use strtotime() function coupled with date() function for this task

for eg. 1 day before 2012-10-09 is

echo date('Y-m-d', strtotime("2012-10-09 -1 day"));
simply-put
  • 1,068
  • 1
  • 11
  • 20
0

Building on Wisdom's answer, you can find the 1st day of the week in a couple steps. Check out the PHP date() manual for more options, but I believe the following code will work:

// figure out how many days back you have to go, to get to Sunday
$d = date('N', strtotime($mydate));
// figure out Sunday's date
$beginning_of_week = date('Y-m-d', strtotime($mydate." -{$d} days"));
$end_of_week = date('Y-m-d', strtotime($beginning_of_week." +1 week"));
echo "The week {$beginning_of_week} to {$end_of_week}! ";
Topher Hunt
  • 4,404
  • 2
  • 27
  • 51
  • Thank you Topher, I tend to over complicated my thought process when the solution is very clear exactly as you explained. – derikluv Oct 12 '12 at 16:00
0

Try this function (source: Marty Wallace).

The date you are interested in is $date (in YYYY-MM-DD format), and $rollover in full day format (e.g. Friday).

function getWeeks($date, $rollover)

{
    $cut = substr($date, 0, 8);
    $daylen = 86400;

    $timestamp = strtotime($date);
    $first = strtotime($cut . "00");
    $elapsed = ($timestamp - $first) / $daylen;

    $i = 1;
    $weeks = 1;

    for($i; $i<=$elapsed; $i++)
    {
        $dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i);
        $daytimestamp = strtotime($dayfind);

        $day = strtolower(date("l", $daytimestamp));

        if($day == strtolower($rollover))  $weeks ++;
    }

    return $weeks;
}
Community
  • 1
  • 1
Ollie
  • 544
  • 4
  • 22