简体   繁体   中英

How to increase the date in the range of selected date by manually adding days in php

Here my jquery ajax request,

I m trying to send selected date or manually increasing days values to the controller

function myFunction(selected_first_date){
    var   get_my_value = $('#get_my_value').val();
    $.ajax({
              url :"<?php echo base_url();?>index.php/holiday_package_controller/PackageController/dateChangerFunction",
              type:"POST",
              data:{
                 get_my_value:get_my_value,
                 selected_first_date:selected_first_date
              },
              dataType: "json",
              success: function(data){
               //$('#cash_receipt_voucher_date').html(data);
               console.log(data);
               $('#incremented_date').val(data.incremented_date);
              }
        });
}

Controller

$get_my_value = $this->input->post("get_my_value");

$selected_first_date = $this->input->post("selected_first_date");
if(!empty($selected_first_date)){
    for($i=0; $i<=$get_my_value; $i++)
        $repeat = strtotime("+$i day",strtotime($selected_first_date));

    $rdate = date('Y-m-d',$repeat);
    print_r($rdate);
    exit();
}

I like the DateTime class for things like this https://3v4l.org/g9v53 :

<?php

$date = new DateTime('2014-09-18 10:00:00');
$date->modify('+12 hours');
echo $date->format('Y-m-d H:i:s');

2014-09-18 22:00:00

Here are all the relative modification formats from the docs:

https://secure.php.net/manual/en/datetime.formats.relative.php

If the date time you are sending is not in 'Ymd H:i:s' format, then use DateTime::createFromFormat($customFormat, $dateTimeString);

As per your code above, first please remove the exit() from the loop as it will terminate the script in first iteration, so put exit() outside like

for($i=1; $i<=$get_my_value; $i++) {
          $repeat = strtotime("+$i day",strtotime($selected_first_date));
          $rdate = date('Y-m-d',$repeat);
          print_r($rdate);
}
exit();

Also I've changed $i to starts from 1 so it will treat it as +1 Day in strtotime for selected date. Keep print_r($rdate) as it is then check output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM