Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert mysql date to "Days Ago". 1

Status
Not open for further replies.

ruffy

Programmer
Oct 1, 2003
72
US
How do I take a Mysql date,
after I retrieved it into $mysqldate,
and convert it to a number that represents "days ago"?
 
You could do it in your MySQL query with the TO_DAYS function. See for an example.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Code:
function getdaysago($date){
 $then = strtotime($date);
 $diff = time() - $then;
 $days = floor($diff/(60*60*24));
 return $days;
}

 
jpadie - thanks!
For some reason, my include_once "daysago.php" file,
(the name I called your script),
php had trouble finding - even though it resides
in the same directory with my other include_once script "config.php". (see error below).

This forced me to use your code inline.
Then it worked just right.

Might you know why the included file couldn't be found?

When I invoked your script with:
$p5 = getdaysago($p5);
The error was:
function getdaysago($date) { $then = strtotime($date); $diff = time() - $then; $days = floor($diff/(60*60*24)); return $days; }
Fatal error: Call to undefined function getdaysago() in /Applications/MAMP/htdocs/phpQuery_test2/indexHPP.php on line 50
 
perhaps you did not include php tags around the script?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top