-1

I have this php code to calculate number of days between current date and specific date.

 <?php 
$query = mysql_query("SELECT * FROM library_users LEFT JOIN students ON library_users.student_id = students.student_id LEFT JOIN books ON library_users.book_id = books.book_id WHERE library_users.student_id <> ''")or die(mysql_error());
  while($library_users = mysql_fetch_array($query))

        {

    $datefrom = date('d-m-Y');
    $dateto = $library_users['return_date'];


    $datefrom = DateTime::createFromFormat('d-m-Y', $datefrom);
    $dateto = DateTime::createFromFormat('d-m-Y', $dateto);

    $date_dur = $datefrom->diff($dateto);
    $days = $date_dur->format('%d');
                                }

For now the current date is 27-06-2017; when I input tomorrow's date which is 28-06-2017 I'm getting 1 day which is fine, but when I input 27-07-2017(next month) it is giving me 0 days. How can I solve this?

Lenny
  • 15
  • 1
  • 5
  • 1
    Your acceptance record is questionable. – Funk Forty Niner Jun 27 '17 at 15:06
  • You can use print_r($date_dur) to examine the object. A property is days. So, you can set $days = $date_dur->days instead of trying to format it. – kainaw Jun 27 '17 at 15:10
  • Don't use the `mysql_*` functions. They have been deprecated since v5.5 (Jun 2013) and removed since v7.0 (Dec 2015). Instead use the [**mysqli_***](https://secure.php.net/manual/en/book.mysqli.php) or [**PDO**](https://secure.php.net/manual/en/book.pdo.php) functions with [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) and [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). – Alex Howansky Jun 27 '17 at 15:11
  • 1
    Questionable @Fred-ii-? His acceptance record is horrible. @Lenny you need to get in the habit of [accepting answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) which help you to solve your issues. You'll earn points and others will be encouraged to help you. – Jay Blanchard Jun 27 '17 at 21:26
  • I'm new in stack overflow @Fread-ii-, still learning some of the things here, thanks for the point. – Lenny Jun 29 '17 at 07:40

3 Answers3

2

The problem is that you are using the %d format to get the days, but this will give you the difference in days of a month.

Use %a instead:

$days = $date_dur->format('%a');

http://php.net/manual/en/dateinterval.format.php

a ==> Total number of days as a result of a DateTime::diff()

ojovirtual
  • 3,332
  • 16
  • 21
0

dumping your object between 27-06-2017 and 27-07-2017 give me this:

object(DateInterval)#4 (15) { ["y"]=> int(0) ["m"]=> int(1) ["d"]=> int(1) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> int(31) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }

so your are right : it gives you 0 days but 1 month (["m"]=> int(1))

You must use:

$date_dur->days

to get the real days number.

regards.

fxlacroix
  • 557
  • 4
  • 18
0

It works as designed.

DateTime::diff() returns an object of type DateInterval.

A DateInterval object stores the interval in years and subdivisions (months, days, hours, minutes and seconds). You tell it to format() itself using the format "%d".

The %d format specifier means the number of days; not the absolute number of days in the interval but only the days of the last incomplete month.

The format to display the total number of days is "%a":

$days = $date_dur->format('%a');
axiac
  • 68,258
  • 9
  • 99
  • 134