如何获取两个日期之间的所有日期并插入数据库中?

时间:2018-06-20 21:05:20

标签: php datetime foreach

我正在尝试获取两个日期之间的所有日期,并将所有日期插入表中。我该如何实现?

    //current date
    $currentDate = date('Y-m-d');

    //find next monday from current date
    $nextMonday = date('Y-m-d', strtotime('next monday', strtotime($currentDate)));   

    //get last day of current year
    $yearEnd = date('Y-m-d', strtotime('12/31'));

   //get all dates between nextMonday and yearend
   [...]

 foreach ($period as $dateCreated) {
   //insert the dates in db
    $stmt = $conn->prepare("INSERT INTO user_schedule (userId,dateCreated) VALUES (?,?)");
    $stmt ->bind_param('is',$userid,$dateCreated);
    $stmt ->execute();
    $stmt ->close();
 }

3 个答案:

答案 0 :(得分:0)

@Transactional
public interface IClaimRepository extends JpaRepository<Claim, Long> {
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    List<Claim> findByLnId(String LnId);

答案 1 :(得分:0)

$firstDay = mktime(0, 0, 0, $rep_month, 1, $rep_year);

$lastDay = mktime(0, 0, 0, ($rep_month+1), 0, $rep_year);

使用上面的代码并根据您的系统进行修改,您将获得最佳效果。

答案 2 :(得分:0)

这对我来说是完美的,它就像一种魅力。非常简单快捷。

  //current date
    $currentDate = date('Y-m-d');

    //find next monday from current date
    $nextMonday = date('Y-m-d', strtotime('next monday', strtotime($currentDate)));   

    //get last day of current year
    $yearEnd = date('Y-m-d', strtotime('12/31'));

 //get all dates between nextMonday and yearEnd
    $period = new DatePeriod(new DateTime($nextMonday), new DateInterval('P1D'), new DateTime($yearEnd .' +1 day'));

    //iterate over each date
    foreach ($period as $date) {

      //format date
      $dateCreated = $dates[] = $date->format("Y-m-d");

   //insert the dates in db
    $stmt = $conn->prepare("INSERT INTO user_schedule (userId,dateCreated) VALUES (?,?)");
    $stmt ->bind_param('is',$userid,$dateCreated);
    $stmt ->execute();
    $stmt ->close();
 }