duanjiao3754 2016-03-21 10:54
浏览 263
已采纳

如何检查php中两个日期之间是否有周末?

I want to check if there is any sat or sun between two dates in php.

$fromDate = '03/21/2016';
$toDate = '03/28/2016';

I've tried other solution asked here but they didn't worked well. I want it in laravel 5.2 so if there is any easy way to handle this please guide me. thanks!

Update i also want to get if there is weekday(mon-fri) between two dates. i need it because user can select sat and Sunday only so in this condition I've to hide weekday option from him. So what I need is a method which tells me if start and end date has weekend or weekdays or both in it.

  • 写回答

2条回答 默认 最新

  • duanjie2940 2016-03-21 11:11
    关注

    You can use date("N") to get current day of week and add the difference of days between your dates.. If this is bigger or equal to 6 than it's a weekend between or one date is in weekend.

    //Get current day of week. For example Friday = 5
    $day_of_week = date("N", strtotime($fromDate));
    
    $days = $day_of_week + (strtotime($toDate) - strtotime($fromDate)) / (60*60*24)
    //strtotime(...) - convert a string date to unixtimestamp in seconds.
    //The difference between strtotime($toDate) - strtotime($fromDate) is the number of seconds between this 2 dates. 
    //We divide by (60*60*24) to know the number of days between this 2 dates (60 - seconds, 60 - minutes, 24 - hours)
    //After that add the number of days between this 2 dates to day of week. So if first date is friday and days between this 2 dates is: 3 the sum will be 5+3 = 8 and it's bigger than 6 so we know it's a weekend between.
    
    
    
    if($days >= 6){
      //we have a weekend. Because day of week of first date + how many days between this 2 dates are greater or equal to 6 (6=Saturday)
    } else {
      //we don't have a weekend
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 提问应符合社区要求 12月19日

悬赏问题

  • ¥15 加一个K2暂停音乐的功能
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?