dounue6984 2017-01-01 18:50
浏览 151

获取PHP中生日时间范围的上限和下限日期

I am trying to calculate two dates, the date a week before and the date eight weeks after a user´s birthday.

In our example we would like to grant the user access to some specials in a timeframe around his birthday. 1 week before his birthday until 8 weeks after his birthday

e.g. #1

User birthday: 1988-01-05
Timeframe should be: YYYY-12-29 to YYYY-03-03

Current Date:  
1) 2016-12-30 
2) 2017-01-01 
3) 2017-02-01
4) 2016-12-28
5) 2017-12-28

Result should be:
1) True
2) True
3) True
4) False
5) False

e.g. #2
User birthday: 1988-12-30
Timeframe should be: YYYY-12-23 to YYYY-02-24

Current Date: 
1) 2016-12-30 
2) 2017-01-01 
3) 2017-02-01
4) 2016-12-28
5) 2017-12-28

Result should be:
1) True
2) True
3) True
4) True
5) True

Has anyone tried to accomplish something similar and can help me out here with some basic example, to light the bulb over my head?

  • 写回答

1条回答 默认 最新

  • dtcd27183 2017-01-01 18:55
    关注

    This can be easily achieved with the carbon library.

    $dt = Carbon::create(1991, 1, 31, 0); // Set the birthday
    $dt->addWeeks(8); // Gives you a date 8 weeks after
    $dt->subWeek(); // Gives you a date 1 week before
    

    And then you don't have to deal with crazy date manipulation which is a huge pain.

    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线