Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get date from float number in php or jquery

Tags:

jquery

date

php

I am working on a project in which i have to Generate a Date from Selected Option value.

Example:

  1. If selected 1.5 then need a date after 1 Month 15 days 15-08-2017
  2. If selected 1.0 then need a date after 1 Month 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 have also tried to get the date in PHP but i didn't get it.

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

So can anyone help me to get date using this float numbers in PHP or Jquery.

like image 830
always-a-learner Avatar asked Dec 20 '25 16:12

always-a-learner


2 Answers

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

you can use this. for 1 month

or

for adding 1 day

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

for adding more than one day $final_date = date("Y-m-d", strtotime("+15 day", 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);
like image 193
Exprator Avatar answered Dec 22 '25 05:12

Exprator


you can add days instead of month in option value. so if 1.5 month 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 feature date.

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);

This will help you.

like image 33
Shyam Shingadiya Avatar answered Dec 22 '25 05:12

Shyam Shingadiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!