Jump to content

Archived

This topic is now archived and is closed to further replies.

shocker-z

date - days ago

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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.