Page 1 of 1

Age of record in days since added

Posted: Sat Apr 18, 2015 11:11 am
by JoeSoaper
I am trying to write report that will give me the age of the record in days. My report looks like this so far

$row .= '<tr>
<td>'.$joined_date.'</td>
<td>'.$registration_number.'</td>
<td>'.$name.'</td>
<td>'.$city.'</td>
<td>'.$AgeDate.'</td>
</tr>';

I need $AgeDate to show the age of the record in days

Joined date is this format: 2015-04-10

date_default_timezone_set('Africa/Johannesburg');
$joined_date= date("Y-m-d ");

Any suggestions will be greatly appreciated

Re: Age of record in days since added

Posted: Sat Apr 18, 2015 2:25 pm
by Christopher
Try the DATEDIFF() function in your SQL statement to return the number of days between NOW() and the date in the field.

Re: Age of record in days since added

Posted: Sat Apr 18, 2015 2:29 pm
by JoeSoaper
I have tried various methods with that all to no avail. I am very new to PHP but this one seems to have me stumped after several days and much Googling.

Re: Age of record in days since added

Posted: Sun Apr 19, 2015 12:30 am
by Christopher

Code: Select all

date_default_timezone_set('Africa/Johannesburg');
$joined_date= date("Y-m-d ");
$sql = "SELECT DATEDIFF('$joined_date', mydatefield) AS days";
// do your query 

Re: Age of record in days since added

Posted: Thu Apr 23, 2015 4:27 pm
by Pazuzu156
If you don't have a problem using a library, I'd look into Carbon. Here is a link to the GitHub page for you.

https://github.com/briannesbitt/Carbon

Re: Age of record in days since added

Posted: Sat Apr 25, 2015 4:29 pm
by Christopher
You can do the same diff functions with PHP's DateTime object.