{"id":10802,"date":"2015-07-08T11:00:04","date_gmt":"2015-07-08T16:00:04","guid":{"rendered":"https:\/\/getflywheel.com\/?p=10802"},"modified":"2023-02-28T12:19:08","modified_gmt":"2023-02-28T18:19:08","slug":"displaying-time-since-posted-wordpress","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/","title":{"rendered":"Displaying &#8216;Time Since Posted&#8217; on WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this post we\u2019re going to create a function that displays time since a post was posted. The <code>human_time_diff()<\/code>&nbsp;function&nbsp;allows you to get the difference between two given times in a readable format; for example: 5 minutes, 10 hours, 9 days, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This mapping is often found in social networks such as Twitter and Facebook, instead of the date of publication. This allows readers to understand the &#8220;freshness&#8221; of the content quickly, without thinking about what day it is.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This time format has increasingly appeared&nbsp;in&nbsp;themes for WordPress. This makes it easy to use the built-in WordPress function&nbsp;<code>human_time_diff()<\/code> to display time since posted on WordPress.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Arguments<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Function&nbsp;<code>human_time_diff()<\/code>&nbsp;takes only two arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$From<\/code>&nbsp;&#8211; the mandatory argument, the beginning of the time range<\/li>\n\n\n\n<li><code>$To<\/code>&nbsp;&#8211; the end of the time range, which is the current time by default<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Both arguments use time in the timestamp format &nbsp;&#8211; the number of seconds since January 1, 1970.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PHP and WordPress have built-in values for the function. For PHP use <code>time()<\/code>&nbsp;and&nbsp;<code>mktime()<\/code>, and for WordPress use&nbsp;<code>get_the_time()&nbsp;<\/code>&nbsp;and&nbsp;<code>current_time()<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2015\/07\/time1.jpg\" alt=\"an analog clock on a table covered in paper covered in typed prose\" class=\"wp-image-10817\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The function&nbsp;<code>human_time_diff()<\/code>&nbsp;can return the difference between the two marks as a number of seconds, minutes, hours, days, weeks, months, or years.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo human_time_diff(time() - 60); \/\/ 1 minute\n\necho human_time_diff(time() - 3600); \/\/ 1 hour\n\necho human_time_diff(time() - 259200); \/\/ 3 days<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Please note that these examples do not specify the second argument&nbsp;$to. In this case, the mark uses WordPress current time using the&nbsp;<code>time()<\/code>&nbsp;function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s look at a simple example that uses <code>human_time_diff()<\/code> to display our new time-since metric on a post by editing the Loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while (have_posts()) {\n\nthe_post();\n\n\/\/ ... This header, content, etc.\n\n&amp;amp;nbsp;\n\n$ Human_time = human_time_diff(get_the_time('U'), current_time ('timestamp'));\n\nprintf('%s Posted ago.', $human_time);\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Function&nbsp;<code>get_the_time()<\/code>&nbsp;allows you to record the timestamp published in the required format, but please note that this function uses the time zone settings that can be changed under Settings\u2192 General in the administration panel WordPress.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That is why&nbsp;we set&nbsp;the second argument&nbsp;to the function&nbsp;<code>human_time_diff()<\/code>&nbsp;and used the&nbsp;built-in&nbsp;WordPress&nbsp;function&nbsp;<code>current_time()<\/code>, which takes&nbsp;into account&nbsp;the time zone setting,&nbsp;unlike&nbsp;the default&nbsp;<code>time()<\/code>&nbsp;function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In other words, make sure that both values in&nbsp;<code>human_time_diff()<\/code>&nbsp;are in the same time zone. You can also use the&nbsp;function <code>get_post_time()<\/code>&nbsp;instead of&nbsp;<code>get_the_time()<\/code>. It allows you to mark time in the time zone GMT\/UTC:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$Human_time = human_time_diff(get_post_time('U', true));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can specify the second argument to the function&nbsp;<code>human_time_diff()<\/code>&nbsp;as the default function of&nbsp;<code>time()<\/code>, which also returns the timestamp in the time zone GMT\/UTC.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2015\/07\/time2.jpg\" alt=\"an analog clock in a public building\" class=\"wp-image-10818\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Many search engine spiders pay special attention to the date of the publication, but the format &#8220;x&nbsp;&nbsp;days ago&#8221; they do not perceive. Because of this, search engine optimization often uses micro-formats.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$Human_time = human_time_diff(get_post_time('U', true));\n\n$Iso8601_time = get_the_time('c');\n\nprintf ('&amp;lt;span class = \"entry-date published\" datetime = \"% s\"&amp;gt; Posted by% s ago. &amp;lt;\/ span&amp;gt;', $iso8601_time, $human_time);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With this, visitors will see the date in a convenient format&nbsp;<code>human_time_diff()<\/code>&nbsp;and search engines can read the date in ISO 8601 format.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The last example is a conclusion date format if the article was published over a week&nbsp;ago:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$Human_time = human_time_diff(get_post_time('U', true));\n\n$Regular_time = get_the_time(get_option('date_format'));\n\n$Output_time = sprintf('%s ago', $human_time);\n\n&amp;amp;nbsp;\n\nif (time()&amp;gt; get_post_time('U', true) + 7 * DAY_IN_SECONDS)\n\n$Output_time = $regular_time;\n\nprintf ('Posted on%s.', $output_time);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the article has been published for more than seven days,&nbsp;this code&nbsp;will display the&nbsp;date in the format&nbsp;that is set&nbsp;in the site settings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Function\u00a0<code>human_time_diff()<\/code>\u00a0can be found in the\u00a0file\u00a0<a href=\"https:\/\/core.trac.wordpress.org\/browser\/trunk\/src\/wp-includes\/formatting.php\" target=\"_blank\" rel=\"noopener noreferrer\">WP-includes\/formatting.php<\/a>. It uses the function of the location and the presence of any\u00a0translation packages for WordPress and copes well with the plural form of numbers (1 day, 2 days, 5 days). To learn more about the functions <code>get_the_time()<\/code> and <code>get_post_time()<\/code>, you can read the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_the_time\/\" target=\"_blank\" rel=\"noreferrer noopener\">Codex<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we\u2019re going to create a function that displays time since a post was posted. The human_time_diff()&nbsp;function&nbsp;allows you to get the difference between two given times in a readable format; for example: 5 minutes, 10 hours, 9 days, etc. This mapping is often found in social networks such as Twitter and Facebook, instead<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":1,"featured_media":140972,"template":"","resource-topic":[1396,901],"resource-role":[1397,896],"resource-type":[916],"class_list":["post-10802","resource","type-resource","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Displaying \u2018time since posted\u2019 on WordPress<\/title>\n<meta name=\"description\" content=\"Help your audience better understand the &quot;freshness&quot; of your content by creating a function that displays time since a post was published.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Displaying \u2018time since posted\u2019 on WordPress\" \/>\n<meta property=\"og:description\" content=\"Help your audience better understand the &quot;freshness&quot; of your content by creating a function that displays time since a post was published.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Engine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpengine\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-28T18:19:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2015\/07\/time-since-header.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@wpengine\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/\",\"url\":\"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/\",\"name\":\"Displaying \u2018time since posted\u2019 on WordPress\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\"},\"datePublished\":\"2015-07-08T16:00:04+00:00\",\"dateModified\":\"2023-02-28T18:19:08+00:00\",\"description\":\"Help your audience better understand the \\\"freshness\\\" of your content by creating a function that displays time since a post was published.\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wpengine.com\/case-studies\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resources\",\"item\":\"https:\/\/wpengine.com\/case-studies\/resources\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Displaying &#8216;Time Since Posted&#8217; on WordPress\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\",\"url\":\"https:\/\/wpengine.com\/case-studies\/\",\"name\":\"WP Engine\",\"description\":\"Managed Hosting for WordPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085\",\"name\":\"WP Engine\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"caption\":\"WP Engine\"},\"sameAs\":[\"https:\/\/wpengine.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Displaying \u2018time since posted\u2019 on WordPress","description":"Help your audience better understand the \"freshness\" of your content by creating a function that displays time since a post was published.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"Displaying \u2018time since posted\u2019 on WordPress","og_description":"Help your audience better understand the \"freshness\" of your content by creating a function that displays time since a post was published.","og_url":"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2023-02-28T18:19:08+00:00","og_image":[{"width":1100,"height":500,"url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2015\/07\/time-since-header.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@wpengine","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/","url":"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/","name":"Displaying \u2018time since posted\u2019 on WordPress","isPartOf":{"@id":"https:\/\/wpengine.com\/case-studies\/#website"},"datePublished":"2015-07-08T16:00:04+00:00","dateModified":"2023-02-28T18:19:08+00:00","description":"Help your audience better understand the \"freshness\" of your content by creating a function that displays time since a post was published.","breadcrumb":{"@id":"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/case-studies\/resources\/displaying-time-since-posted-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/case-studies\/"},{"@type":"ListItem","position":2,"name":"Resources","item":"https:\/\/wpengine.com\/case-studies\/resources\/"},{"@type":"ListItem","position":3,"name":"Displaying &#8216;Time Since Posted&#8217; on WordPress"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/case-studies\/#website","url":"https:\/\/wpengine.com\/case-studies\/","name":"WP Engine","description":"Managed Hosting for WordPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085","name":"WP Engine","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","caption":"WP Engine"},"sameAs":["https:\/\/wpengine.com"]}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2015\/07\/time-since-grid.png","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":"<strong>Roles:<\/strong> Designer, Developer","topic":"<strong>Topics:<\/strong> Design, WordPress","_links":{"self":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource\/10802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource"}],"about":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/types\/resource"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/users\/1"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media\/140972"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media?parent=10802"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-topic?post=10802"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-role?post=10802"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-type?post=10802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}