Comparing Time Function
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I need a PHP function to convert a time string (with the formatMM/DD/YYYY H:MM and then AM or PM at the end). It's very similar to this bounty I was looking at, but I need something with more rules.

Rules

  1. If time is in same hour, return x minutes ago
  2. If within six hours, return x hours ago
  3. If greater than six hours, but within the same day, return HH:MM AM or HH:MM PM
  4. If one day before, return Yesterday
  5. If within one week, return the day (eg. Monday)
  6. For everything longer than a week, print out the day (eg. 1/30/2013)

All times are in PDT. I will tip for a method in Objective-C/Cocoa that would work in an iOS app and another in Java (for use in an Android app). (You do not need to be the original solution provider for this, a translation is fine)

awarded to tomtoump

Crowdsource coding tasks.

2 Solutions

Winning solution

<?php

function relativeTime($time) {
    $now  = time();
    $then = strtotime($time);
    $diff = $now - $then;

    if ($diff < 60 * 60 - 30) {
        $diff = round($diff / 60);
        return $diff == 1 ? $diff . ' minute ago' : $diff . ' minutes ago';
    }
    else if ($diff <= 6 * 60 * 60 + 60) {
        $diff = round($diff / (60 * 60));
        return $diff == 1 ? $diff . ' hour ago' : $diff . ' hours ago';
    }
    else if (date('Ymd', $then) == date('Ymd', $now)) {
        return date('h:i A', $then);
    }
    else if (date('Ymd', $then) == date('Ymd', strtotime("yesterday"))) {
        return 'Yesterday';
    }
    else if ($then >= strtotime("-7 days")) {
        return date('l', $then);
    }
    else {
        return date('n/j/Y', $then);
    }
}

?>

You should check NSDateFormatter for iOS and getRelativeDateTimeString for Android.

If you're returning in each conditional, you don't need to use else.
slang over 9 years ago
That's why you didn't copy that part? :P Is there any performance gain?
tomtoump over 9 years ago
Lol, copy it? That would be pretty hard since I posted my answer before you. I actually modeled my answer off of the code that Stack Overflow uses here. I don't think there is a major performance gain, it just makes more sense in terms of flow-control and is a little shorter.
slang over 9 years ago
I was referring to the if about the third rule and the links for iOS/Android.
tomtoump over 9 years ago

Try this, it should be able to be rewritten in Java and Objective-C pretty easily:

<?php
$SECOND = 1;
$MINUTE = 60 * $SECOND;
$HOUR = 60 * $MINUTE;
$DAY = 24 * $HOUR;
$MONTH = 30 * $DAY;

function format_time($timestamp) {
    $seconds = $timestamp - $time();

    if ($seconds < 0) {
        return "not yet";
    }
    $minutes = floor($seconds / $MINUTE);
    if ($minutes < 1) {
        return $seconds == 1 ? "one second ago" : $seconds + " seconds ago";
    }
    if ($minutes < 2) {
        return "a minute ago";
    }
    if ($minutes < 45) {
        return $minutes + " minutes ago";
    }
    if ($minutes < 90) {
        return "an hour ago";
    }
    $hours = floor($seconds / $HOUR);
    if ($hours < 6) {
        return $hours + " hours ago";
    }
    if (date("Ymd", $timestamp) == date("Ymd")) {
        return date('h:i A', $timestamp);
    }
    if ($hours < 48) {
        return "yesterday";
    }
    $days = floor($seconds / $DAY);
    if ($days < 7) {
        return date("l", $timestamp);
    }
    return date("Y-m-d", $timestamp); // http://xkcd.com/1179/
}
?>

Oh, and MM/DD/YYYY H:MM is a really terrible format. Never use that. Ever. Use Unix time-stamps for everything. It will make your life much easier, and that's what the above function accepts. As for Objective-C, use NSDateFormatter and for Java (assuming you're using Android) use getRelativeDateTimeString.

View Timeline