Age of record in days since added

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JoeSoaper
Forum Newbie
Posts: 2
Joined: Sat Apr 18, 2015 11:04 am

Age of record in days since added

Post 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
User avatar
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

Post by Christopher »

Try the DATEDIFF() function in your SQL statement to return the number of days between NOW() and the date in the field.
(#10850)
JoeSoaper
Forum Newbie
Posts: 2
Joined: Sat Apr 18, 2015 11:04 am

Re: Age of record in days since added

Post 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.
User avatar
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

Post 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 
(#10850)
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Age of record in days since added

Post 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
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
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

Post by Christopher »

You can do the same diff functions with PHP's DateTime object.
(#10850)
Post Reply