Page 1 of 1

getting number of days out of dates

Posted: Sat Aug 26, 2006 6:40 pm
by GeXus
could someone tell me how you would get the number of days from a given date... for example on blogs, if you make a comment.. in two days.. it will say "Two Days Ago"... etc

Thanks!

Posted: Sat Aug 26, 2006 6:44 pm
by Chris Corbyn

Code: Select all

//just for this test.. use a real date in actuality
$test_date = strtotime('2 days ago');

$seconds_in_day = 60 * 60 * 24;

time() - $test_date = $difference;

$days = abs($difference);

echo "$days days ago";

Posted: Sat Aug 26, 2006 6:48 pm
by Chris Corbyn
OK time for me to go to bed... I'm getting scrappy:

Code: Select all

<?php

//just for this test.. use a real date in actuality
$test_date = strtotime('2 days ago');

$seconds_in_day = 60 * 60 * 24;

$difference = time() - $test_date;

$days = floor($difference/$seconds_in_day);

echo "$days days ago";

?>

Posted: Sat Aug 26, 2006 7:21 pm
by RobertGonzalez
To wrap it into a function...

Code: Select all

<?php
function get_days_since($date)
{
    $seconds_in_day = 60 * 60 * 24;

    $since_date = strtotime($date);

    $seconds_since = time() - $since_date;

    $days_since = floor($seconds_since/$seconds_in_days);

    return $days_since;
}

//Usage
$date_to_check = 'January 3, 2005';
$days =  get_days_since($date_to_check);

echo 'It has been ' . $days . ' days since ' . $date_to_check . '.';
?>

Posted: Sat Aug 26, 2006 8:09 pm
by GeXus
Nice, thanks guys!