{"id":9456,"date":"2021-01-18T16:56:29","date_gmt":"2021-01-18T07:56:29","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9456"},"modified":"2021-01-18T10:23:28","modified_gmt":"2021-01-18T01:23:28","slug":"calculation-of-time-elapsed-in-php","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9456","title":{"rendered":"Calculation of time elapsed in PHP"},"content":{"rendered":"\n<p>Today I would like to write some code about calculating time elapsed in PHP. Especially if we want to know the elapsed time of inserted records (seconds, minutes, hours , days etc).<\/p>\n\n\n\n<p>For example &#8211; Posted 1 \u9031\u524d\u30013 \u6642\u9593\u524d\u300150 \u5206\u9593\u524d\u300130\u79d2\u9593\u524d\u3002<\/p>\n\n\n\n<p>Here is our function to calculate the time eclipse<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nfunction time_elapsed($datetime) {\n    $now = new DateTime;\n    $ago = new DateTime($datetime);\n    \/\/getting the time differences.\n    $diff = $now->diff($ago);\n\n    $diff->w = floor($diff->d \/ 7); \/\/week\n    $diff->d -= $diff->w * 7; \/\/day\n\n    \/\/string arrays of formats\n    $string = array(\n        'y' => '\u5e74',\n        'm' => '\u6708',\n        'w' => '\u9031',\n        'd' => '\u65e5',\n        'h' => '\u6642\u9593',\n        'i' => '\u5206',\n        's' => '\u79d2',\n    );\n\n    \/\/mapping the time difference and format\n    foreach ($string as $k => &amp;$v) {\n        if ($diff->$k) {\n            $v = $diff->$k . ' ' . $v; \n        } else {\n            unset($string[$k]);\n        }\n    }\n\n    \/\/only getting the first array key and value\n    $string = array_slice($string, 0, 1);\n    return $string ? implode(', ', $string) . '\u524d' : '\u3061\u3087\u3046\u3069\u4eca';\n}<\/code><\/pre>\n\n\n\n<p>I have included the comments in code so that you can be able to understand it. Let&#8217;s call our function to test.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo time_elapsed(\"2020-12-06 11:24:25\"); \/\/output 1 \u6708\u524d\necho time_elapsed(date(\"Y-m-d H:i:s\")); \/\/output \u3061\u3087\u3046\u3069\u4eca<\/code><\/pre>\n\n\n\n<p>PS. we have to care about our server timezone when we are dealing with Datetime functions<\/p>\n\n\n\n<p>By Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9456\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9456\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Calculation of time elapsed in PHP\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9456\" data-text=\"Calculation of time elapsed in PHP\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9456\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9456\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Today I would like to write some code about calculating time elapsed in PHP. Especially if we want to know the [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9456"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9456"}],"version-history":[{"count":3,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9456\/revisions"}],"predecessor-version":[{"id":9462,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9456\/revisions\/9462"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}