1

I need to convert DateTime into the year, month, days, hours, minutes, seconds ago like as yahoo question asked 5week ago, or 1 year ago or 1 month ago, my date is saved in the database like this: 2011-11-30 05:25:50.

Now I want to show like year, month, days, hours, minutes and seconds in PHP like: how much year, how many months , days, hours, minutes, seconds ago, but need only one time unit show like it will be year or days or hours or minutes or seconds.

I have searched about this but not understand how I can do this, I know there are many questions at this topic on Stack Overflow, maybe it is duplicate but I am unable to solve my issue.

Thanks in advance.

Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130

4 Answers4

5

Assuming you mean relative to the present (if you don't, please clarify your question), you could use this:

$then = new DateTime('2011-11-30 05:25:50');
$now = new DateTime();
$delta = $now->diff($then);

$quantities = array(
    'year' => $delta->y,
    'month' => $delta->m,
    'day' => $delta->d,
    'hour' => $delta->h,
    'minute' => $delta->i,
    'second' => $delta->s);

$str = '';
foreach($quantities as $unit => $value) {
    if($value == 0) continue;
    $str .= $value . ' ' . $unit;
    if($value != 1) {
        $str .= 's';
    }
    $str .=  ', ';
}
$str = $str == '' ? 'a moment ' : substr($str, 0, -2);

echo $str;

Output:

1 year, 9 months, 17 days, 14 hours, 31 minutes, 31 seconds
George Brighton
  • 5,131
  • 9
  • 27
  • 36
  • They clarified, they don't. [See this answer](http://stackoverflow.com/questions/18787790/difference-between-2-time-values/18787809#18787809) for a more concise way to do this. – John Conde Sep 17 '13 at 19:01
  • I have question table there is save asked_date 2011-11-30 05:25:50 now I want to show this question was asked 'year,month,days,hours,minutes,seconds ago – Muhammad Shahzad Sep 17 '13 at 19:09
  • 2
    Retrieve that timestamp from the database in the normal way, put `$result['asked_date']` into the constructor of the first `DateTime` (`$then`), and append ` ago` to the final `$str`. – George Brighton Sep 17 '13 at 19:19
  • 2
    Yes - passing no arguments initialises the DateTime object to the current timestamp. – George Brighton Sep 17 '13 at 19:46
  • Thank you what I was want,but now can you edit your answer please as if there is no year,no month and no days it shows 0 year,0 month so on,but I want to only that values that are exist mean if there are seconds then return me second , I mean if else condition :) – Muhammad Shahzad Sep 17 '13 at 19:52
  • 2
    There's a much easier way - see my edit. `if($value == 0) continue;` – George Brighton Sep 17 '13 at 20:26
  • Saved my life. Thanks a lot George !! – Ismail May 30 '15 at 04:19
1

I've always used this function:

function when($datetime) {   

    define("SECOND", 1);
    define("MINUTE", 60 * SECOND);
    define("HOUR", 60 * MINUTE); define("DAY", 24 * HOUR);
    define("MONTH", 30 * DAY); $delta = time() - strtotime($datetime);

    // convert

    if($delta < 1 * MINUTE) { return $delta == 1 ? "one second ago" : $delta." seconds ago"; }
    if($delta < 2 * MINUTE) { return "a minute ago"; } if($delta < 45 * MINUTE) { return floor($delta / MINUTE)." minutes ago"; }
    if($delta < 90 * MINUTE) { return "an hour ago"; } if($delta < 24 * HOUR) { return floor($delta / HOUR)." hours ago"; }
    if($delta < 48 * HOUR) { return "yesterday"; } if($delta < 30 * DAY) { return floor($delta / DAY)." days ago"; }
    if($delta < 12 * MONTH) { $months = floor($delta / DAY / 30); return $months <= 1 ? "one month ago" : $months." months ago"; }
    else { $years = floor($delta / DAY / 365); return $years <= 1 ? "one year ago" : $years." years ago"; }

}

echo when(YOUR DATE HERE) will return the best format of relative time, which might be days, hours, or if it wasn't that long ago, even in seconds.

Daniel Schwarz
  • 284
  • 1
  • 10
0

Try this: http://www.php.net/manual/en/function.strtotime.php

PHP has a nice function strtotime all ready for your needs.

That'll give you a timestamp, then you can just do math from there from whatever date you are subtracting from.

Esaevian
  • 1,707
  • 5
  • 18
  • 30
  • But that's it, they're *not* doing math on two different dates – John Conde Sep 17 '13 at 18:48
  • I made an assumption that they had a second date ready to go. Still, you can convert a single date to a string like that if you want something like `2011 years, 11 months, 30 days, 5 hours, 25 minutes, 50 seconds` – Esaevian Sep 17 '13 at 18:49
  • Hmph, nevermind, I just saw ali's comment. Now this question makes no sense. – Esaevian Sep 17 '13 at 18:53
  • I don't want difference. – Muhammad Shahzad Sep 17 '13 at 18:53
  • @ali Then your question makes no sense. What it the logic of converting "2011" to "2 years" for instance? – Esaevian Sep 17 '13 at 18:56
  • I have question table there is save asked_date `2011-11-30 05:25:50` now I want to show this question was asked 'year,month,days,hours,minutes,seconds ago, – Muhammad Shahzad Sep 17 '13 at 19:09
  • 1
    Ah! "ago" is the magic word. For that you do want a difference between now. now() - asked_date = time ago. Use `strtotime()` to get the asked_date time, use `now()` to get the current time, subtract now from asked_date to get the difference between the time in seconds, then do basic math to figure out the different in different time units. – Esaevian Sep 17 '13 at 19:12
  • 1
    Or, more simply, `date_diff()` -> http://www.php.net/manual/en/function.date-diff.php Check the examples/comments – Esaevian Sep 17 '13 at 19:14
0

You could try splitting this using preg_split.

Try something like

$output = preg_split( "/ (-| |:) /", $input );

That should give you an array, where the first element is the year, second is the month, and so on.

mellypop
  • 66
  • 1
  • 8