dongxkbjiaofea1163
2016-03-05 10:53
浏览 65
已采纳

PHP为一周中的每一天生成时间戳

I'm trying to generate a timestamp for each day of the week. My week starts Mon and ends Sun.

My code so far is below. Problem is that today (Sat), the generated timestamp for Sun is "last sunday", but it should be "next sunday".

I'm not sure what the best way is without using loads of if / else statements.

$today = time();
    //$today = strtotime('next monday');

    // If today is monday, generate timestamps for each day of the week starting today
    if(date('D', $today) == 'Mon') {
        $mon = $today;
        $tue = strtotime('next tuesday');
        $wed = strtotime('next wednesday');
        $thu = strtotime('next thursday');
        $fri = strtotime('next friday');
        $sat = strtotime('next saturday');
        $sun = strtotime('next sunday');
    }
    // Today is not monday, so generate timestamps for each day of the week, starting last monday
    else {
        $mon = strtotime('last monday');
        $tue = (date('D', $today) == 'Tue') ? $today : strtotime('last tuesday');
        $wed = (date('D', $today) == 'Wed') ? $today : strtotime('last wednesday');
        $thu = (date('D', $today) == 'Thu') ? $today : strtotime('last thursday');
        $fri = (date('D', $today) == 'Fri') ? $today : strtotime('last friday');
        $sat = (date('D', $today) == 'Sat') ? $today : strtotime('last saturday');
        $sun = (date('D', $today) == 'Sun') ? $today : strtotime('last sunday');
    }

图片转代码服务由CSDN问答提供 功能建议

我正在尝试为一周中的每一天生成时间戳。 我的一周从周一开始,结束了太阳。

到目前为止我的代码如下。 问题是今天(星期六),太阳的生成时间戳是“最后一个星期天”,但它应该是“下一个星期天”。

如果不使用if / else语句的加载,我不确定最好的方法是什么。

  $ today = time()  ; 
 // $ today = strtotime('next monday'); 
 
 //如果今天是星期一,则从今天开始为每周的每一天生成时间戳
 if(date('D',$ today)  =='周一'){
 $ mon = $ today; 
 $ tue = strtotime('下周二'); 
 $ wed = strtotime('next wednesday'); 
 $ thu = strtotime('next 星期四'); 
 $ fri = strtotime('下周五'); 
 $ sat = strtotime('下周六'); 
 $ sun = strtotime('next sunday'); 
} 
 // 今天不是星期一,所以生成一周中每一天的时间戳,从上周一开始
其他{
 $ mon = strtotime('last monday'); 
 $ tue =(date('D',$ today)  =='星期二')?  $ today:strtotime('last tuesday'); 
 $ wed =(date('D',$ today)=='Wed')?  $ today:strtotime('last wednesday'); 
 $ thu =(date('D',$ today)=='Thu')?  $ today:strtotime('last thursday'); 
 $ fri =(date('D',$ today)=='Fri')?  $ today:strtotime('last friday'); 
 $ sat =(date('D',$ today)=='Sat')?  $ today:strtotime('last saturday'); 
 $ sun =(date('D',$ today)=='Sun')?  $ today:strtotime('last sunday'); 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyingjiu0669 2016-03-05 11:21
    已采纳
    $now = time();
    
    if(date('D', $now) == 'Mon') {
        $mon = mktime(0,0,0);
    } else {
        $mon = strtotime('last monday');
    }
    $tue = strtotime('+1 day', $mon);
    $wed = strtotime('+1 day', $tue);
    $thu = strtotime('+1 day', $wed);
    $fri = strtotime('+1 day', $thu);
    $sat = strtotime('+1 day', $fri);
    $sun = strtotime('+1 day', $sat);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题