Adamhumbug Posted September 4, 2023 Share Posted September 4, 2023 (edited) I have a database field that has a datetime in it of 2023-09-04 18:01:50.000000. When i try and work out how long ago that was - get a negative number. I have tried several "borrowed" functions to work this out and none of them are giving me what i expect. This is what i am using now: function get_time_ago($time_stamp) { $time_difference = strtotime('now') - $time_stamp; if ($time_difference >= 60 * 60 * 24 * 365.242199) { /* * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 365.242199 days/year * This means that the time difference is 1 year or more */ return get_time_ago_string($time_stamp, 60 * 60 * 24 * 365.242199, 'year'); } elseif ($time_difference >= 60 * 60 * 24 * 30.4368499) { /* * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 30.4368499 days/month * This means that the time difference is 1 month or more */ return get_time_ago_string($time_stamp, 60 * 60 * 24 * 30.4368499, 'month'); } elseif ($time_difference >= 60 * 60 * 24 * 7) { /* * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 7 days/week * This means that the time difference is 1 week or more */ return get_time_ago_string($time_stamp, 60 * 60 * 24 * 7, 'week'); } elseif ($time_difference >= 60 * 60 * 24) { /* * 60 seconds/minute * 60 minutes/hour * 24 hours/day * This means that the time difference is 1 day or more */ return get_time_ago_string($time_stamp, 60 * 60 * 24, 'day'); } elseif ($time_difference >= 60 * 60) { /* * 60 seconds/minute * 60 minutes/hour * This means that the time difference is 1 hour or more */ return get_time_ago_string($time_stamp, 60 * 60, 'hour'); } else { /* * 60 seconds/minute * This means that the time difference is a matter of minutes */ return get_time_ago_string($time_stamp, 60, 'minute'); } } function get_time_ago_string($time_stamp, $divisor, $time_unit) { $time_difference = strtotime("now") - $time_stamp; $time_units = floor($time_difference / $divisor); settype($time_units, 'string'); if ($time_units === '0') { return 'less than 1 ' . $time_unit . ' ago'; } elseif ($time_units === '1') { return '1 ' . $time_unit . ' ago'; } else { /* * More than "1" $time_unit. This is the "plural" message. */ // TODO: This pluralizes the time unit, which is done by adding "s" at the end; this will not work for i18n! return $time_units . ' ' . $time_unit . 's ago'; } } when i echo what is being fed to this function i see 2023-09-04 18:01:04 and when strtotime i get 1693850464 the function is using now to work out the current time so i dont understand why it thinks my date is in the future. Edited September 4, 2023 by Adamhumbug Quote Link to comment Share on other sites More sharing options...
Adamhumbug Posted September 4, 2023 Author Share Posted September 4, 2023 Hi, Can this please be closed. I dont know what i have done but it is now working - i cannot really give any more info. Could an admin please close this as it is not a helpful post. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 4, 2023 Share Posted September 4, 2023 Suggestions: 1. function timeDifference($datetime) { $periods = ['yrs', 'mths', 'days', 'hrs', 'mins', 'secs']; $dt1 = new DateTime($datetime); $dt2 = new DateTime(); $d = $dt1->diff($dt2); $diffs = array_filter(array_combine($periods, [ $d->y, $d->m, $d->d, $d->h, $d->i, $d->s ] )); $res = $d->invert ? '-' : ''; foreach ($diffs as $k => $v) { $res .= "$v $k "; } return $res; } echo "Now: " . date('Y-m-d H:i:s') . '<br>'; // Now: 2023-09-04 18:54:22 echo timeDifference('2023-09-04 19:00:00.000000'); // -5 mins 37 secs 2. You can also utilise SQL functions SELECT now() as Now , timestampdiff(SECOND, now(), '2023-09-04 19:00:00.000000') as Diff; +---------------------+------+ | Now | Diff | +---------------------+------+ | 2023-09-04 19:03:34 | -214 | +---------------------+------+ SELECT now() as Now , timediff('2023-09-04 19:00:00.000000', now()) as diff; +---------------------+------------------+ | Now | diff | +---------------------+------------------+ | 2023-09-04 19:08:39 | -00:08:39.000000 | +---------------------+------------------+ Quote Link to comment Share on other sites More sharing options...
requinix Posted September 5, 2023 Share Posted September 5, 2023 7 hours ago, Adamhumbug said: when i echo what is being fed to this function i see 2023-09-04 18:01:04 You mean $time_stamp is that string value? Well, there's your problem. Also, the code you have there just isn't very good. Barand's solution(s) are simpler and make more sense. 7 hours ago, Adamhumbug said: Could an admin please close this as it is not a helpful post. Might not be helpful to you, but it could be helpful to someone else in the future who has a similar problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.