Print Y-m-d list of business dates between two dates from MySQL using PHP

后端 未结 2 806
旧巷少年郎
旧巷少年郎 2021-01-28 00:45

I have this MySQL table:

desc studentabsence;
+---------------------------+-------------+
| Field                     | Type        |
+-------------         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-28 01:05

    // Date strings from DB
    $startDate = '2012-08-01';
    $endDate = '2012-08-08';
    
    // Convert to UNIX timestamps
    $currentTime = strtotime($startDate);
    $endTime = strtotime($endDate);
    
    // Loop until we reach the last day
    $result = array();
    while ($currentTime <= $endTime) {
      if (date('N', $currentTime) < 6) {
        $result[] = date('Y-m-d', $currentTime);
      }
      $currentTime = strtotime('+1 day', $currentTime);
    }
    
    // Show the result
    // You could loop the array to pretty-print it, or do it within the above loop
    print_r($result);
    

    See it working

提交回复
热议问题