note 21268 deleted from function.date by sniper
| From: | sniper@php.net | Date: | Sat, 18 Jan 2003 01:34:51 +0000 |
| Subject: | note 21268 deleted from function.date by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-42321@lists.php.net to get a copy of this message | ||
I have been noticing a lot of discussion on how to avoid using the date() function to format the
proper date, and also much discussion on how to subvert the strtotime() function, but I havent seen
much discussion on how to find how many days have passed since a certain date/time. I have prepared
a tiny bit of code to demonstrate this for those that may be in need.
This actually checks between the seconds that pass so that it will be correct no matter what day of
the year the original date is.
<?
//date can be any value that strtotime
//will recognize. I am using the mssql
//format of: "2001-01-01 00:00:00.000"
//for this example, so i have to chop off
//the last four characters
//get the pretend date from the db
$tmp = ("2002-05-02 01:01:00.000");
// remove the last 4 characters
$stored_date = substr($tmp,0,strlen($tmp)-4);
//save memory!! (not much, but every byte counts)
unset ($tmp);
// get the value of the stored date
$date_secs = strtotime ($stored_date);
// get the value of right now
$now = time();
// compute the difference
$timediff = $date_secs - $now;
//get the int val of the days passed
//$secondspassed = intval(abs(((($timediff/60)/60)/24)));
$dayspassed = intval(abs(((($timediff/60)/60)/24)));
//now to use it in a real case:
//Tell the user how long they have been
//a member of your site...
echo "<p align=left>you have been a member for $dayspassed days\n";
//or you can use it to see if a
//subscription has ended
//Length of subscription
$subscription_days = 14;
//The user's subscription is about to end,
//let them know...
if (($subscription_days -1 == $dayspassed ) || ($subscription_days == $dayspassed)) {
echo "<p align=left>Your subscription is about to end, you may renew <a
href=\"\">here</a>\n";
return (1);
}
// The user still has time left, and isnt
// about to expire, so tell them.
if ( $subscription_days > $dayspassed ) {
$daysleft = $subscription_days - $dayspassed;
echo "<p align=left>You have ($daysleft) days left in your subscription\n";
return (1);
} else {
// Otherwise tell them that their account has
// expired and where to renew it.
echo "<p align=left>Your subscription has ended, please renew <a
href=\"\">here</a>\n";
return (0);
}
?>
I hope this helps someone out there. If it does, please drop me a line letting me know! Happy
coding :)