Jump to content


Photo

date - days ago


  • Please log in to reply
3 replies to this topic

#1 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 29 August 2006 - 01:09 PM

how do i use date() funtione to calculate how many days ago the input date was? i tryed just calculatin it but YYYYMMDD but fell into issue of last month being 100+ days ago because it is 20060728 do i use ticks instead or is there a better function that's all singing and dancing?
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 29 August 2006 - 01:14 PM

how are you trying to calculate it? a typical, readable date format is YYYY-MM-DD with the dashes. however, for it to be readable by the date() function, you've got to change it to a timestamp with strtotime() or something similar. basically, here's a good way to calculate it:
<?php
$date = "2006-04-01"; // april 1st of this year
$today = date('Y-m-d');
$diff = abs(strtotime($today) - strtotime($date));
$days = ceil($diff / (60 * 60 * 24));
echo "$days Days since date provided.";
?>

hope this helps.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 29 August 2006 - 01:16 PM

Use mktime() (IE mktime(0,0,0,month,day,year);) to create a time stamp for the old date.
Then decrease from time() the result you get, devide by 60*60*24, and round it using floor() or ceil().

Orio.
Think you're smarty?

(Gone until 20 to November)

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 29 August 2006 - 01:26 PM

cheers obsidian that's working a treat! :D
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users