douzhu6149 2012-10-12 15:40
浏览 8
已采纳

too long

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.

  • 写回答

4条回答 默认 最新

  • dtah63820 2012-10-12 16:44
    关注
    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 
     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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效