$TimeNow examples

//Get the db DateTime (function below)
$SqlServerTimeNow = db_read_server_time();

//Get the PHP server DateTiem now (not necessarily the same as the SQL server!!)
$TimeNow = date("Y-m-d H:i:s");



//******************************************
//******************************************
//********** READ SQL SERVER TIME **********
//******************************************
//******************************************
function db_read_server_time ()
{
  global $wpdb;
  
  $sql ="SELECT NOW() as SqlServerDateTimeNow";
  if (current_user_can('administrator'))
    $wpdb->show_errors();
  $Results = $wpdb->get_results($sql, ARRAY_A);
  if (count($Results) > 0)
    return($Results[0]['SqlServerDateTimeNow']);
  else
    return("");     
}

User friendly how long ago

//****************************************************
//****************************************************
//********** GET USER FRIENDLY HOW LONG AGO **********
//****************************************************
//****************************************************
function GetUserFriendlyHowLongAgo ($TimeNow, $PastDateTime)
{
  
  $SecondsElapsed = strtotime($TimeNow) - strtotime($PastDateTime);
  $MinutesElapsed = floor($SecondsElapsed / 60);      //floor() rounds down so you get only full minutes that have passed
  $HoursElapsed = floor($SecondsElapsed / 3600);
  $DaysElapsed = floor($SecondsElapsed / 86400);
  $YearsElapsed = floor($DaysElapsed / 365);

  if ($SecondsElapsed < 0)
    return("");     //In the future - invalid
  else if ($YearsElapsed > 1)
    return("$YearsElapsed years ago");
  else if ($YearsElapsed == 1)
    return("$YearsElapsed year ago");
  else if ($DaysElapsed > 1)
    return("$DaysElapsed days ago");
  else if ($DaysElapsed == 1)
    return("$DaysElapsed day ago");
  else if ($HoursElapsed > 1)
    return("$HoursElapsed hours ago");
  else if ($HoursElapsed == 1)
    return("$HoursElapsed hour ago");
  else if ($MinutesElapsed > 1)
    return("$MinutesElapsed mins ago");
  else 
    return("Just now");
}
Multi language version
//Requires the db_read_server_time() function above

//****************************************************
//****************************************************
//********** GET USER FRIENDLY HOW LONG AGO **********
//****************************************************
//****************************************************
function GetUserFriendlyHowLongAgo ($TimeNow, $PastDateTime)
{
  
  $SecondsElapsed = strtotime($TimeNow) - strtotime($PastDateTime);
  $MinutesElapsed = floor($SecondsElapsed / 60);      //floor() rounds down so you get only full minutes that have passed
  $HoursElapsed = floor($SecondsElapsed / 3600);
  $DaysElapsed = floor($SecondsElapsed / 86400);
  $YearsElapsed = floor($DaysElapsed / 365);

  if ($SecondsElapsed < 0)
    return("");     //In the future - invalid
  else if ($YearsElapsed > 1)
    return("$YearsElapsed " . __('years ago', 'mytd') );
  else if ($YearsElapsed == 1)
    return("$YearsElapsed " . __('year ago', 'mytd') );
  else if ($DaysElapsed > 1)
    return("$DaysElapsed " . __('days ago', 'mytd') );
  else if ($DaysElapsed == 1)
    return("$DaysElapsed " . __('day ago', 'mytd') );
  else if ($HoursElapsed > 1)
    return("$HoursElapsed " . __('hours ago', 'mytd') );
  else if ($HoursElapsed == 1)
    return("$HoursElapsed " . __('hour ago', 'mytd') );
  else if ($MinutesElapsed > 1)
    return("$MinutesElapsed " . __('mins ago', 'mytd') );
  else 
    return(__('Just now', 'mytd') );
}

User friendly how long until

//Requires the db_read_server_time() function above


//******************************************************
//******************************************************
//********** GET USER FRIENDLY HOW LONG UNTIL **********
//******************************************************
//******************************************************
function GetUserFriendlyHowLongUntil ($TimeNow, $FutureDateTime)
{
  
  $SecondsElapsed = strtotime($FutureDateTime) - strtotime($TimeNow);
  $MinutesElapsed = floor($SecondsElapsed / 60);      //floor() rounds down so you get only full minutes that have passed
  $HoursElapsed = floor($SecondsElapsed / 3600);
  $DaysElapsed = floor($SecondsElapsed / 86400);
  $YearsElapsed = floor($DaysElapsed / 365);

  if ($YearsElapsed > 1)
    return("$YearsElapsed years");
  else if ($YearsElapsed == 1)
    return("$YearsElapsed year");
  else if ($DaysElapsed > 1)
    return("$DaysElapsed days");
  else if ($DaysElapsed == 1)
    return("$DaysElapsed day");
  else if ($HoursElapsed > 1)
    return("$HoursElapsed hours");
  else if ($HoursElapsed == 1)
    return("$HoursElapsed hour");
  else if ($MinutesElapsed > 1)
    return("$MinutesElapsed mins");
  else 
    return("past");
}
Multi language version
//Requires the db_read_server_time() function above

//******************************************************
//******************************************************
//********** GET USER FRIENDLY HOW LONG UNTIL **********
//******************************************************
//******************************************************
function GetUserFriendlyHowLongUntil ($TimeNow, $FutureDateTime)
{
  
  $SecondsElapsed = strtotime($FutureDateTime) - strtotime($TimeNow);
  $MinutesElapsed = floor($SecondsElapsed / 60);      //floor() rounds down so you get only full minutes that have passed
  $HoursElapsed = floor($SecondsElapsed / 3600);
  $DaysElapsed = floor($SecondsElapsed / 86400);
  $YearsElapsed = floor($DaysElapsed / 365);

  if ($YearsElapsed > 1)
    return("$YearsElapsed " . __('years', 'mytd') );
  else if ($YearsElapsed == 1)
    return("$YearsElapsed " . __('year', 'mytd') );
  else if ($DaysElapsed > 1)
    return("$DaysElapsed " . __('days', 'mytd') );
  else if ($DaysElapsed == 1)
    return("$DaysElapsed " . __('day', 'mytd') );
  else if ($HoursElapsed > 1)
    return("$HoursElapsed " . __('hours', 'mytd') );
  else if ($HoursElapsed == 1)
    return("$HoursElapsed " . __('hour', 'mytd') );
  else if ($MinutesElapsed > 1)
    return("$MinutesElapsed " . __('mins', 'mytd') );
  else 
    return(__('past', 'mytd') );
}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.