FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Check for Birthdays with time()





moejoe
Hi.

Is there a way to effectively detect birthday(s) from your sql datebase using time() or date()

They're birthdays are currently saved in a unix timestamp.

Smile Thanks in advance.
rvec
sure check for timestamps like this:

Code:

$a = mktime (0, 0 ,0) ;
$b = mktime (23, 59 ,59);
$query = "SELECT * FROM table WHERE birthday > '$a' AND birthday < '$b'"
moejoe
Thanks.

damnit its soo simple!


Oh and while we're on the subject of time..

How can i work out how long it's been from a set time stamp.

For instance i have a timestamp dated the 1st of April. And now it is the 3rd of April. - how do i tell php to show it as "2 Days ago"... something like that.

Thanks.
rvec
the timestamp is stored in $a. You probably know how to get it from the database or some other source in there.
Code:

$time = time(); //this is the current time
$diff = $time - $a; //this is the difference in seconds between now and the timestamp stored in $a
if ($diff < 0) {
    echo 'The given time is in the future';
} else {
    if ($diff > (60*60*24*7)) {
        $diff = round($diff / (60*60*24*7)); //make diff a time in weeks
        echo 'The given time is '. $diff .' weeks ago.';
    } elseif ($diff > (60*60*24)) {
        $diff = round($diff / (60*60*24)); //make diff a time in days
        echo 'The given time is '. $diff .' days ago.';
    } elseif ($diff > (60*60)) {
        $diff = round($diff / (60*60)); //make diff a time in hours
        echo 'The given time is '. $diff .' hours ago.';
    } else {
        echo 'The given time is '. $diff .' seconds ago.';
    }
}
moejoe
Thanks Smile that hit the spot nicely.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.