{"id":2865,"date":"2020-05-29T06:16:24","date_gmt":"2020-05-29T05:16:24","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2865"},"modified":"2022-05-25T14:32:55","modified_gmt":"2022-05-25T13:32:55","slug":"useful-datetime-functions","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/date-and-time\/useful-code\/useful-datetime-functions","title":{"rendered":"Days until \/ Days Ago"},"content":{"rendered":"\n<p>$TimeNow examples<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Get the db DateTime (function below)\n$SqlServerTimeNow = db_read_server_time();\n\n\/\/Get the PHP server DateTiem now (not necessarily the same as the SQL server!!)\n$TimeNow = date(\"Y-m-d H:i:s\");\n\n\n\n\/\/******************************************\n\/\/******************************************\n\/\/********** READ SQL SERVER TIME **********\n\/\/******************************************\n\/\/******************************************\nfunction db_read_server_time ()\n{\n  global $wpdb;\n  \n  $sql =\"SELECT NOW() as SqlServerDateTimeNow\";\n  if (current_user_can('administrator'))\n    $wpdb-&gt;show_errors();\n  $Results = $wpdb-&gt;get_results($sql, ARRAY_A);\n  if (count($Results) &gt; 0)\n    return($Results&#91;0]&#91;'SqlServerDateTimeNow']);\n  else\n    return(\"\");     \n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">User friendly how long ago<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/****************************************************\n\/\/****************************************************\n\/\/********** GET USER FRIENDLY HOW LONG AGO **********\n\/\/****************************************************\n\/\/****************************************************\nfunction GetUserFriendlyHowLongAgo ($TimeNow, $PastDateTime)\n{\n  \n  $SecondsElapsed = strtotime($TimeNow) - strtotime($PastDateTime);\n  $MinutesElapsed = floor($SecondsElapsed \/ 60);      \/\/floor() rounds down so you get only full minutes that have passed\n  $HoursElapsed = floor($SecondsElapsed \/ 3600);\n  $DaysElapsed = floor($SecondsElapsed \/ 86400);\n  $YearsElapsed = floor($DaysElapsed \/ 365);\n\n  if ($SecondsElapsed &lt; 0)\n    return(\"\");     \/\/In the future - invalid\n  else if ($YearsElapsed > 1)\n    return(\"$YearsElapsed years ago\");\n  else if ($YearsElapsed == 1)\n    return(\"$YearsElapsed year ago\");\n  else if ($DaysElapsed > 1)\n    return(\"$DaysElapsed days ago\");\n  else if ($DaysElapsed == 1)\n    return(\"$DaysElapsed day ago\");\n  else if ($HoursElapsed > 1)\n    return(\"$HoursElapsed hours ago\");\n  else if ($HoursElapsed == 1)\n    return(\"$HoursElapsed hour ago\");\n  else if ($MinutesElapsed > 1)\n    return(\"$MinutesElapsed mins ago\");\n  else \n    return(\"Just now\");\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Multi language version<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Requires the db_read_server_time() function above\n\n\/\/****************************************************\n\/\/****************************************************\n\/\/********** GET USER FRIENDLY HOW LONG AGO **********\n\/\/****************************************************\n\/\/****************************************************\nfunction GetUserFriendlyHowLongAgo ($TimeNow, $PastDateTime)\n{\n  \n  $SecondsElapsed = strtotime($TimeNow) - strtotime($PastDateTime);\n  $MinutesElapsed = floor($SecondsElapsed \/ 60);      \/\/floor() rounds down so you get only full minutes that have passed\n  $HoursElapsed = floor($SecondsElapsed \/ 3600);\n  $DaysElapsed = floor($SecondsElapsed \/ 86400);\n  $YearsElapsed = floor($DaysElapsed \/ 365);\n\n  if ($SecondsElapsed &lt; 0)\n    return(\"\");     \/\/In the future - invalid\n  else if ($YearsElapsed &gt; 1)\n    return(\"$YearsElapsed \" . __('years ago', 'mytd') );\n  else if ($YearsElapsed == 1)\n    return(\"$YearsElapsed \" . __('year ago', 'mytd') );\n  else if ($DaysElapsed &gt; 1)\n    return(\"$DaysElapsed \" . __('days ago', 'mytd') );\n  else if ($DaysElapsed == 1)\n    return(\"$DaysElapsed \" . __('day ago', 'mytd') );\n  else if ($HoursElapsed &gt; 1)\n    return(\"$HoursElapsed \" . __('hours ago', 'mytd') );\n  else if ($HoursElapsed == 1)\n    return(\"$HoursElapsed \" . __('hour ago', 'mytd') );\n  else if ($MinutesElapsed &gt; 1)\n    return(\"$MinutesElapsed \" . __('mins ago', 'mytd') );\n  else \n    return(__('Just now', 'mytd') );\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">User friendly how long until<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Requires the db_read_server_time() function above\n\n\n\/\/******************************************************\n\/\/******************************************************\n\/\/********** GET USER FRIENDLY HOW LONG UNTIL **********\n\/\/******************************************************\n\/\/******************************************************\nfunction GetUserFriendlyHowLongUntil ($TimeNow, $FutureDateTime)\n{\n  \n  $SecondsElapsed = strtotime($FutureDateTime) - strtotime($TimeNow);\n  $MinutesElapsed = floor($SecondsElapsed \/ 60);      \/\/floor() rounds down so you get only full minutes that have passed\n  $HoursElapsed = floor($SecondsElapsed \/ 3600);\n  $DaysElapsed = floor($SecondsElapsed \/ 86400);\n  $YearsElapsed = floor($DaysElapsed \/ 365);\n\n  if ($YearsElapsed &gt; 1)\n    return(\"$YearsElapsed years\");\n  else if ($YearsElapsed == 1)\n    return(\"$YearsElapsed year\");\n  else if ($DaysElapsed &gt; 1)\n    return(\"$DaysElapsed days\");\n  else if ($DaysElapsed == 1)\n    return(\"$DaysElapsed day\");\n  else if ($HoursElapsed &gt; 1)\n    return(\"$HoursElapsed hours\");\n  else if ($HoursElapsed == 1)\n    return(\"$HoursElapsed hour\");\n  else if ($MinutesElapsed &gt; 1)\n    return(\"$MinutesElapsed mins\");\n  else \n    return(\"past\");\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Multi language version<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Requires the db_read_server_time() function above\n\n\/\/******************************************************\n\/\/******************************************************\n\/\/********** GET USER FRIENDLY HOW LONG UNTIL **********\n\/\/******************************************************\n\/\/******************************************************\nfunction GetUserFriendlyHowLongUntil ($TimeNow, $FutureDateTime)\n{\n  \n  $SecondsElapsed = strtotime($FutureDateTime) - strtotime($TimeNow);\n  $MinutesElapsed = floor($SecondsElapsed \/ 60);      \/\/floor() rounds down so you get only full minutes that have passed\n  $HoursElapsed = floor($SecondsElapsed \/ 3600);\n  $DaysElapsed = floor($SecondsElapsed \/ 86400);\n  $YearsElapsed = floor($DaysElapsed \/ 365);\n\n  if ($YearsElapsed &gt; 1)\n    return(\"$YearsElapsed \" . __('years', 'mytd') );\n  else if ($YearsElapsed == 1)\n    return(\"$YearsElapsed \" . __('year', 'mytd') );\n  else if ($DaysElapsed &gt; 1)\n    return(\"$DaysElapsed \" . __('days', 'mytd') );\n  else if ($DaysElapsed == 1)\n    return(\"$DaysElapsed \" . __('day', 'mytd') );\n  else if ($HoursElapsed &gt; 1)\n    return(\"$HoursElapsed \" . __('hours', 'mytd') );\n  else if ($HoursElapsed == 1)\n    return(\"$HoursElapsed \" . __('hour', 'mytd') );\n  else if ($MinutesElapsed &gt; 1)\n    return(\"$MinutesElapsed \" . __('mins', 'mytd') );\n  else \n    return(__('past', 'mytd') );\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>$TimeNow examples User friendly how long ago Multi language version User friendly how long until Multi language version<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[249],"tags":[],"class_list":["post-2865","post","type-post","status-publish","format-standard","hentry","category-useful-code"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2865"}],"version-history":[{"count":9,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2865\/revisions"}],"predecessor-version":[{"id":4281,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2865\/revisions\/4281"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}