How to count the number of days between two dates and display the result in a text box in php

<li>Date From *<br /> <input type="text" name="date_from" id="date_from" value="<?=gf_dateformat($date_from,'Ym-d') ?>" onclick="javascript: displayDatePicker('date_from', false, 'dmy', '-');" style="width:100px;" /> </li> <li>Date TO *<br /> <input type="text" name="date_to" id="date_to" value="<?=gf_dateformat($date_to,'Ym-d') ?>" onclick="javascript: displayDatePicker('date_to', false, 'dmy', '-');" style="width:100px;" onblur="" /> </li> <li>No of Days <br /> <input type="text" name="num_days" id="num_days" required="yes" validate='text' title="Enter Number of Days"/> </li> 
+4
source share
3 answers

this way you can find the difference of two dates in JS.

  var Days = Math.floor((Date2.getTime() - Date1.getTime())/(1000*60*60*24)); 

in php you can use this code ...

 $startTimeStamp = strtotime("2011/07/01"); $endTimeStamp = strtotime("2011/07/17"); $timeDiff = abs($endTimeStamp - $startTimeStamp); $numberDays = $timeDiff/86400; // 86400 seconds in one day // and you might want to convert to integer $numberDays = intval($numberDays); 
0
source

There are two ways to calculate the difference between dates in PHP. Procedural and object oriented.

Procedural style

     $ datetime1 = date_create ('YYYY-mm-dd');
     $ datetime2 = date_create ('YYYY-mm-dd');
     $ interval = date_diff ($ datetime1, $ datetime2);
     echo $ interval-> format ('% R% a days');

    

Object Oriented Style

$ datetime1 = new DateTime ('YYYY-mm-dd'); $ datetime2 = new DateTime ('YYYY-mm-dd'); $ interval = $ datetime1-> diff ($ datetime2); echo $ interval-> format ('% R% a days');

You can use any of them.

0
source

Try the function below. Works great:

 /** * Calculates the difference between two dates in days, months and years. * @param $start_date string Starting date. * @param $end_date string Ending date. * @return string Specifying the days, months and years in difference. * @category Date */ function get_date_diff($start_date, $end_date) { $diff = abs(strtotime($end_date) - strtotime($start_date)); $years = floor($diff / (365*60*60*24)); $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); return "{$days} days, {$months} months, {$years} years"; } 
0
source

Source: https://habr.com/ru/post/1500436/


All Articles