Get date from float number in php or jquery

I am working on a project where I need to create a date from a selected parameter value.

Example:

  • If selected , a date after 1 month 15 days is required 1.5

    15-08-2017

  • If selected , a date after 1 month is required 1.0

    17-08-2017

    <select class="form-control" name="days" id="days" required>
        <option data-days="1.5" value="3">After 1 month 15 days</option>
        <option data-days="2.5" value="4">After 2 month 15 days</option>
        <option data-days="6.0" value="10">After 5 month</option>
        <option data-days="7.5" value="30">After 7 month 15 days</option>       
        //And So on...
    </select>
    
    // Here is the script for getting selected option days attribute.
    $('body').on('change', '#days', function(e) {
        var days = $(this).find(':selected').data('days');
    
        // Now How do i get the date according the days. as shown in example
    
    });
    
          

I also tried to get the date at PHP

, but I didn't get it.

$date = date('Y-m-d', strtotime('+1.0 month'));

      

So can someone help me get the date using these numbers float

in PHP

or Jquery

.

+3


source to share


2 answers


$final = date("Y-m-d", strtotime("+1 month", $time));

      

you can use this. in 1 month

or

to add 1 day



$final = date("Y-m-d", strtotime("+1 day", $time));

      

to add more than one day $ final_date = date ("Ymd", strtotime ("+ 15 days", strtotime ($ date)));

$float_number = '4.5'; 

$nubers = explode('.', $float_number); 

$date = date('Y-m-d', strtotime('+'.$nubers[0].' month')); 

if($nubers[1] == '0'){ 

$final_date = $date; 

} else { 

$final_date = date("Y-m-d", strtotime("+15 day", strtotime($date))); 

} 

print_r("Final Date: ".$final_date);

      

+1


source


you can add days instead of months to the parameter value. so if 1.5 months than add 45 days (1.5 * 30).

<option data-days="45" value="3">After 1 month 15 days</option>
<option data-days="75" value="4">After 2 month 15 days</option>
<option data-days="180" value="10">After 5 month</option>
<option data-days="225" value="30">After 7 month 15 days</option>  

      

And inside PHP, you can add days for the date of the function.

date("Y-m-d", strtotime("+30 days", $time));

      



In javascript, you can use below solution.

    var time = new Date();
    time.setDate(time.getDate()+30);
    console.log(time);
      

Run codeHide result


This will help you.

+1


source







All Articles