Skip to content

Instantly share code, notes, and snippets.

@jabes
Last active September 4, 2020 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabes/bf989f3fea48d594dcd5 to your computer and use it in GitHub Desktop.
Save jabes/bf989f3fea48d594dcd5 to your computer and use it in GitHub Desktop.
PHP Relative Date
# Copyright (c) 2020 Justin Bull
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
function getRelativeTime($time)
{
$hour = date("H", $time);
if ($hour == 12) return " @ Noon";
else if ($hour == 0) return " @ Midnight";
else return " @ " . date("g:i A", $time);
}
function getRelativeDate($date_str, $direction = "past", $show_time = true)
{
$time = strtotime($date_str);
$today = date('Ymd');
$today_m = date('YmdHi');
$today_s = date('YmdHis');
$date_Ymd = date('Ymd', $time);
$date_YmdHi = date('YmdHi', $time);
$date_YmdHis = date('YmdHis', $time);
$formatted_date = null;
switch ($direction) {
case "past":
// current date is greater than the provided date
// AND current date -1 week is less than the provided date
// range: NOW -1 week <-- DATE --> NOW
$condition_week = $today > $date_Ymd && date('Ymd', strtotime('-1 week')) < $date_Ymd ? true : false;
break;
case "future":
// current date is less than the provided date
// AND current date +1 week is greater than the provided date
// range: NOW <-- DATE --> NOW +1 week
$condition_week = $today < $date_Ymd && date('Ymd', strtotime('+1 week')) > $date_Ymd ? true : false;
break;
}
// is the date within a second from now?
if ($today_s == $date_YmdHis) {
$formatted_date = 'Just Now';
// is the date within a minute from now?
} elseif ($today_m == $date_YmdHi) {
$time_diff = intval($today_s - $date_YmdHis);
$time_descriptor = ($time_diff > 1) ? 'Seconds' : 'Second';
$formatted_date = sprintf("%s %s Ago", $time_diff, $time_descriptor);
// is the date today?
} elseif ($today == $date_Ymd) {
$formatted_date = 'Today';
if ($show_time) $formatted_date .= getRelativeTime($time);
// is the date yesterday?
} elseif (date('Ymd', strtotime('-1 day')) == $date_Ymd) {
$formatted_date = 'Yesterday';
if ($show_time) $formatted_date .= getRelativeTime($time);
// is the date tomorrow?
} elseif (date('Ymd', strtotime('+1 day')) == $date_Ymd) {
$formatted_date = 'Tomorrow';
if ($show_time) $formatted_date .= getRelativeTime($time);
// is the date within the past/future week?
} elseif ($condition_week) {
// ex: Friday 2:52 pm
$formatted_date = date('l', $time);
if ($show_time) $formatted_date .= getRelativeTime($time);
// is the date year within the current year?
} elseif (date('Y') == date('Y', $time)) {
// ex: June 6th 2:52 pm
$formatted_date = date('F jS', $time);
if ($show_time) $formatted_date .= getRelativeTime($time);
// is the date year greater than the current year?
} else {
// ex: 2013-08-25 2:52 pm
$formatted_date = date('Y-m-d', $time);
if ($show_time) $formatted_date .= getRelativeTime($time);
}
return $formatted_date;
}
@walesmd
Copy link

walesmd commented Sep 2, 2020

@jabes Would it be possible for you to update this Gist with an appropriate license reflecting your intent in sharing (maybe the MIT or GPL)? The lack of a license inherently gives you full copyright, with no implied right of others to make use of this code.

If your intent is not for others to be able to make use of this, that would be welcome information as well.

@jabes
Copy link
Author

jabes commented Sep 4, 2020

@walesmd I added the MIT license, and you're free to use it as you wish. However, I wrote this in 2013 so it may no longer be up to date with modern PHP standards. For example, if I were to rewrite this, I would use the more recent Datetime class, I would use type declarations, and I would wrap these functions in a class for PSR4 autoloading standards. But if this does what you need, then I'm happy for whoever gets some use out of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment