doulao3078 2012-12-25 00:00
浏览 48

计算一年中某一天的特定日期

Initially I am trying to create a function to display how many times does a specific day fall into particular date. for example how many times does Saturday fall into January 1st of certain year to certain year.

<?php 

$firstDate = '01/01/2000';
$endDate = '01/01/2012';
$newYearDate= '01/01';

# convert above string to time
$time1 = strtotime($firstDate);
$time2 = strtotime($endDate);
$newYearTime = strtotime($newYearDate);

for($i=$time1; $i<=$time2; $i++){
    $saturday = 0;
    $chk = date('D', $newYearTime); #date conversion
   if($chk == 'Sat' && $chk == $newYearTime){ 
      $saturday++;    
      } 
}
echo $saturday;

?>
  • 写回答

2条回答 默认 最新

  • drhgzx4727 2012-12-25 00:24
    关注

    strtotime gives you seconds since 1970-01-01. Since you're interested in days only, you can increment your loop by 86400 seconds per day to speed up your calculation

    for($i = $time1; $i <= $time2; $i += 86400) {
    ...
    }
    

    There are several points

    • move $saturday out of your loop
    • check new year's eve with day of year
    • check the loop counter $i instead of $newYearTime

    This should work

    $firstDate = '01/01/2000';
    $endDate = '01/01/2012';
    
    # convert above string to time
    $time1 = strtotime($firstDate);
    $time2 = strtotime($endDate);
    
    $saturday = 0;
    for($i=$time1; $i<=$time2; $i += 86400){
        $weekday = date('D', $i);
        $dayofyear = date('z', $i);
        if($weekday == 'Sat' && $dayofyear == 0){ 
            $saturday++;    
        } 
    }
    
    echo "$saturday
    ";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题