2

Okay, so I know how to format a 'datetime' value from my database with PHP. Now, I would like to display the time differently for posts, posted with different amounts of time passed.

So for instance if the time difference between now and the date for the post is:

Less than an hour - Display Number of Minutes Ago More Than Hour But less that 24 hours - Display Number of Hours Ago 24 Hours or more But Less Than 48 hours - Display "Yesterday" and The time 48 hours or more - Display The day, month and the time

Here is what i have so far:

$date = date_create($row['date']); 
$HeaderDate = date_format(date_create($row['date']), 'F j, g:i a'); 
echo $HeaderDate;

This doesn't test any conditions, it just echo's the month, day and time no matter the duration of time that has passed.

Now, I just have no idea how to calculate the differences in date to use in my if statements. How do I do that? But In just PHP I don't want to use SQL at all, other than querying the original date.

Thanks

user1592953
  • 135
  • 2
  • 9
  • 1
    What you need is to use [`DateTime`](http://php.net/manual/ru/class.datetime.php), [`DateTime::diff`](http://php.net/manual/ru/datetime.diff.php) and [`DateInterval`](http://php.net/manual/ru/class.dateinterval.php). – Nemoden Sep 20 '12 at 04:00
  • More dupes of this than I know of... http://stackoverflow.com/questions/2915864/php-how-to-find-the-time-elapsed-since-a-date-time http://stackoverflow.com/questions/2020517/calculating-the-time-difference-in-an-php-mysql-javascript-system http://stackoverflow.com/questions/2690504/php-producing-relative-date-time-from-timestamps – donutdan4114 Sep 20 '12 at 04:46

3 Answers3

0

I've used this function to display time in nice format. Hope this will help you.

I think you've to modify a bit in-order to get it in your way. As you want it to show the actual time after two days.

<?php
function nicetime($date)
{
    if(empty($date)) {
        return "No date provided";
    }

    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");

    $now             = time();
    $unix_date         = strtotime($date);

       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";

    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }

    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }

    $difference = round($difference);

    if($difference != 1) {
        $periods[$j].= "s";
    }

    return "$difference $periods[$j] {$tense}";
}

$date = "2012-09-18 17:45";
$result = nicetime($date); // 2 days ago

?>
Vins
  • 8,849
  • 4
  • 33
  • 53
0

I will show you the function I use. You have to modify it for having all your cases you need. You can see a minimal example here

function timeToString($ptime) { // $ptime should be timestamp
    $etime = time() - $ptime;

    if ($etime <= 30)
        return 'few seconds ago';

    $a = array(
        10 * 12 * 30 * 24 * 60 * 60  =>  'decade',
        12 * 30 * 24 * 60 * 60       =>  'year',
        30 * 24 * 60 * 60            =>  'month',
        7 * 24 * 60 * 60             =>  'week',
        24 * 60 * 60                 =>  'day',
        60 * 60                      =>  'hour',
        60                           =>  'minute',
        1                            =>  'second'
         );

        foreach ($a as $secs => $str) {
            $d = $etime / $secs;
            if ($d >= 1) {
                $r = floor($d);
                if($etime > 7 * 24 * 60 * 60) {
                    $ret = 'on %1$s, %2$s%3$s';
                    return sprintf($ret,date('F', $ptime), date('d', $ptime),(date('Y') != date('Y', $ptime) ? ' ' . date('Y', $ptime) : ''));
                }
                $ret = '%d ' . $str . ($r > 1 ? 's' : '') . ' ago';

                return sprintf($ret,$r);
        }
    }
}
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
0

if you are using type: timestamp and default current_timestamp() in your date column, then this should work fine.

<?php
// change this according to your system's timezone
date_default_timezone_set('Asia/Manila');

$date  = new DateTime(date("Y-m-d H:i:s", strtotime($row['date'])));
$date_now = new DateTime(date("Y-m-d H:i:s"));

// this is used to compare the difference
$diff = $date_now->diff($date)->format('%Y%M%D%H%I%S');

// this is used to show the difference in time with different format
$show_time = $date_now->diff($date);

// 200 is equal to 00 year, 00 month, 00 day, 00 hour, 02 minutes, 00 second
if($diff < 200){ $HeaderDate = "Just now"; }

// 10000 is equal to 00 year, 00 month, 00 day, 01 hour, 00 minute, 00 second
elseif($diff < 10000){ $HeaderDate = $show_time->format("%i minutes ago"); }

// 20000 is equal to 00 year, 00, month 00 day, 02 hours, 00 minute, 00 second
elseif($diff < 20000){ $HeaderDate = "An hour ago"; }

// 1000000 is equal to 00 year, 00 month, 01 day, 00 hour, 00 minute, 00 second
elseif($diff < 1000000){ $HeaderDate = $show_time->format("%h hours ago ".$date->format('(g:i a)')); }

// 2000000 is equal to 00 year, 00 month, 02 days, 00 hour, 00 minute, 00 second
elseif($diff < 2000000){ $HeaderDate = " Yesterday ".$date->format("(g:i a)"); }

// 7000000 is equal to 00 year, 00 month, 07 days, 00 hour, 00 minute, 00 second
elseif($diff < 7000000){ $HeaderDate = $date->format('l (g:i a)'); }

// 100000000 is equal to 00 year, 01 month, 00 day, 00 hour, 00 minute, 00 second
elseif($diff < 100000000){ $HeaderDate = $show_time->format("%d days ago ").$date->format('(M. d, g:i a)'); }

// 2000000 is equal to 00 year, 02 months, 00 day, 00 hour, 00 minute, 00 second
elseif($diff < 200000000){ $HeaderDate = "Last month" . $date->format(' (M. d, g:i a)'); }

// 10000000000 is equal to 01 year, 00 month, 00 day, 00 hour, 00 minute, 00 second
elseif($diff < 10000000000){ $HeaderDate = $show_time->format("%m months ago") . $date->format(' (M. d, g:i a)'); }

else { $HeaderDate = $date->format('M. d, Y g:i a'); }

echo $HeaderDate;
?>
Red
  • 1