Click here to Skip to main content
15,895,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get the age of a record by calculating the difference between two dates in the record. All efforts to date have failed. I am new at this so any help or advice would be greatly appreciated.

I am just starting in PHP so please bear with me. I have tried to do thus myself for 2 weeks but have failed to date. I am doing a report that will give me the age of the record by trying to subtract the created date from the closed date. One of the many codes I have is as follows:

$sql = 'SELECT * FROM table WHERE status="Closed" ';
                       foreach ($pdo->query($sql) as $row) {
                                echo '<tr>';
                                echo '<td>'. $row['id'] . '</td>';
                                echo '<td>'. $row['status'] . '</td>';
                                echo '<td>' (date_diff(. $row['created'] ,  $row['now'] .))'</td>';                              
                                echo '</tr>';
                       }
                       Database::disconnect();
                      ?>


The date_diff is not working and although I have tried numerous variations I just cannot seem to get it to work. Any help or suggestions where I might find the correct solution would be greatly appreciated.

What I have tried:

Everything I know form searching the Internet. I have attempted many solutions but have not yet succeeded
Posted
Updated 14-Jun-18 22:26pm
v2
Comments
Member 13842964 25-May-18 2:39am    
Thank you for that very quick reply, it is very much appreciated. I will try it shortly and give feedback on the result.
Member 13842964 25-May-18 3:47am    
I now get this error:

Warning: date_diff() expects parameter 1 to be DateTime, string given

I have tried some suggested solutions for the Internet but no luck so far

1 solution

Quote:
echo '<td>' (date_diff(. $row['created'] ,  $row['created'] .))'</td>';

Firstly, the "."s are in the wrong place:
echo '<td>' . date_diff($row['created'], $row['created']) . '</td>';

Secondly, you're trying to take the difference between the created column and … the created column. That obviously won't produce a difference, since both values are the same.

If you want the difference between two columns, you need to specify which column you want to compare to.
echo '<td>' . date_diff($row['created'], $row['some-other-column-here']) . '</td>';
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900