{"id":1053,"date":"2011-02-09T04:02:22","date_gmt":"2011-02-09T04:02:22","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=1053"},"modified":"2016-04-04T21:26:40","modified_gmt":"2016-04-04T21:26:40","slug":"how-to-change-date-formate-of-twitter-field-created_at%e2%80%99","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2011\/02\/how-to-change-date-formate-of-twitter-field-created_at%e2%80%99\/","title":{"rendered":"How to change date formate of twitter field &#8220;created_at&#8221;?"},"content":{"rendered":"<p>php coder who is reading twitter API for latest tweets want to change date\/ timestamp format which is returned by \u2018created_at\u2019 key. Most of the times I use JSON based request. e. i. http:\/\/search.twitter.com\/search.json?q=phpmind<\/p>\n<p>These are the fields returned by above request.<\/p>\n<p>Hope fully you understand how json string looks like.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"phpmind-twitter-json\" src=\"http:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2011\/02\/phpmind-twitter-json.jpg\" alt=\"\" width=\"250\" height=\"304\" \/><\/p>\n<p>Now it is time to convert date time stamp in more readable format.<\/p>\n<pre lang=\"php\">echo date(\"l M j \\- g:ia\",strtotime($object-&gt;returns[0]-&gt;created_at));<\/pre>\n<p>Please change the date parameters if you want to play with different date formats.<\/p>\n<p>If you want to use javaScript way to format dates here is very cool function.<br \/>\nReturned by twitter &#8211; Wed, 09 Feb 2011 02:44:36 +0000<\/p>\n<p>Returned by this javaScript function, TwitterDateConverter ($object-&gt;returns[0]-&gt;created_at);<br \/>\nOutput will be for Example <strong>&#8220;14 minutes ago&#8221;<\/strong><\/p>\n<p>See Image Below!<\/p>\n<pre lang=\"js\"><script type=\"text\/JavaScript\">\/\/ <![CDATA[\r\nfunction TwitterDateConverter(time){\r\n\tvar date = new Date(time),\r\n\t\tdiff = (((new Date()).getTime() - date.getTime()) \/ 1000),\r\n\t\tday_diff = Math.floor(diff \/ 86400);\r\n\t\t\t\r\n\tif ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )\r\n\t\treturn;\r\n\t\t\t\r\n\treturn day_diff == 0 && (\r\n\t\t\tdiff < 60 &#038;&#038; \"just now\" ||\r\n\t\t\tdiff < 120 &#038;&#038; \"1 minute ago\" ||\r\n\t\t\tdiff < 3600 &#038;&#038; Math.floor( diff \/ 60 ) + \" minutes ago\" ||\r\n\t\t\tdiff < 7200 &#038;&#038; \"1 hour ago\" ||\r\n\t\t\tdiff < 86400 &#038;&#038; Math.floor( diff \/ 3600 ) + \" hours ago\") ||\r\n\t\tday_diff == 1 &#038;&#038; \"Yesterday\" ||\r\n\t\tday_diff < 7 &#038;&#038; day_diff + \" days ago\" ||\r\n\t\tday_diff < 31 &#038;&#038; Math.ceil( day_diff \/ 7 ) + \" weeks ago\";\r\n}\r\n\/\/ ]]><\/script><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"Twitter-date-formatting\" src=\"http:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2011\/02\/phpmind-date-foramting-example.jpg\" alt=\"\" width=\"484\" height=\"442\" \/><\/p>\n<pre lang=\"php\">\r\n\r\nfunction Convert_Datetime($datetime)\r\n{\r\n    define(\"SECOND\", 1);\r\n    define(\"MINUTE\", 60 * SECOND);\r\n    define(\"HOUR\", 60 * MINUTE); define(\"DAY\", 24 * HOUR);\r\n    define(\"MONTH\", 30 * DAY); $delta = time() - strtotime($datetime);\r\n\r\n    \/\/ convert\r\n\r\n    if($delta < 1 * MINUTE)\r\n    {\r\n        return $delta == 1 ? \"one second ago\" : $delta.\" seconds ago\";\r\n    }\r\n    if($delta < 2 * MINUTE)\r\n    { return \"a minute ago\";\r\n    }\r\n    if($delta < 45 * MINUTE)\r\n    {\r\n        return floor($delta \/ MINUTE).\" minutes ago\";\r\n    }\r\n    if($delta < 90 * MINUTE)\r\n    {\r\n        return \"an hour ago\";\r\n    }\r\n    if($delta < 24 * HOUR)\r\n    {\r\n        return floor($delta \/ HOUR).\" hours ago\";\r\n    }\r\n    if($delta < 48 * HOUR)\r\n    {\r\n        return \"yesterday\";\r\n    }\r\n    if($delta < 30 * DAY)\r\n    {\r\n        return floor($delta \/ DAY).\" days ago\";\r\n    }\r\n    if($delta < 12 * MONTH)\r\n    {\r\n        $months = floor($delta \/ DAY \/ 30); return $months <= 1 ? \"one month ago\" : $months.\" months ago\"; }\r\n    else\r\n    {\r\n        $years = floor($delta \/ DAY \/ 365); return $years <= 1 ? \"one year ago\" : $years.\" years ago\";\r\n    }\r\n\r\necho Convert_Datetime(\"2015-04-1 22:10:00\"); \/\/ one year ago\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>php coder who is reading twitter API for latest tweets want to change date\/ timestamp format which is returned by \u2018created_at\u2019 key. Most of the times I use JSON based request. e. i. http:\/\/search.twitter.com\/search.json?q=phpmind These are the fields returned by above request. Hope fully you understand how json string looks like. Now it is time [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1053","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=1053"}],"version-history":[{"count":10,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1053\/revisions"}],"predecessor-version":[{"id":1586,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1053\/revisions\/1586"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=1053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=1053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=1053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}