Jump to content

date - days ago


shocker-z

Recommended Posts

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?
Link to comment
Share on other sites

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:
[code]
<?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.";
?>
[/code]

hope this helps.
Link to comment
Share on other sites

Use [url=http://www.php.net/manual/en/function.mktime.php]mktime()[/url] (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 [url=http://www.php.net/manual/en/function.floor.php]floor()[/url] or [url=http://www.php.net/manual/en/function.ceil.php]ceil()[/url].

Orio.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.