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
Age of record in days since added
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Age of record in days since added
Try the DATEDIFF() function in your SQL statement to return the number of days between NOW() and the date in the field.
(#10850)
Re: Age of record in days since added
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Age of record in days since added
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 (#10850)
Re: Age of record in days since added
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
https://github.com/briannesbitt/Carbon
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Age of record in days since added
You can do the same diff functions with PHP's DateTime object.
(#10850)