{"id":1279,"date":"2020-02-16T15:48:15","date_gmt":"2020-02-16T08:48:15","guid":{"rendered":"https:\/\/lab.wptips.dev\/?p=1279"},"modified":"2024-07-05T09:52:32","modified_gmt":"2024-07-05T02:52:32","slug":"datetime-object","status":"publish","type":"post","link":"https:\/\/pixelstudio.id\/blog\/datetime-object\/","title":{"rendered":"Working with Date\/Time Object in WordPress"},"content":{"rendered":"\n<p>A year ago I was developing a Scheduling plugin for a client. I soon realized that there are many overlapping Date methods in PHP. Combined with the built-in WordPress methods, I was overwhelmed.<\/p>\n\n\n\n<p>In this article, we will show you the best ways for working with Date object in WordPress.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. DateTime Format<\/h2>\n\n\n\n<p>A quick reference on the common DateTime format. The complete list can be found in  the <a rel=\"noreferrer noopener\" aria-label=\"official PHP doc (opens in a new tab)\" href=\"https:\/\/www.php.net\/manual\/en\/function.date.php\" target=\"_blank\">official PHP doc<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td><strong>Year<\/strong><\/td><td><code>Y<\/code><br><code>y<\/code><\/td><td>2015<br>15<\/td><\/tr><tr><td><strong>Month<\/strong><\/td><td><code>M<\/code><br><code>m<\/code><br><code>n<\/code><br><code>F<\/code><\/td><td>Jan &#8211; Dec<br>01 &#8211; 12<br>1 &#8211; 12<br>January &#8211; December<\/td><\/tr><tr><td><strong>Day<\/strong><\/td><td><code>D<\/code><br><code>d<\/code><br><code>j<\/code><\/td><td>Mon &#8211; Sun<br>01 &#8211; 31<br>1 &#8211; 31<\/td><\/tr><tr><td><strong>Hour<\/strong><\/td><td><code>H<\/code><br><code>h<\/code><br><code>A<\/code><\/td><td>00 &#8211; 23<br>01 &#8211; 12<br>AM or PM<\/td><\/tr><tr><td><strong>Minute<\/strong><\/td><td><code>i<\/code><\/td><td>01 &#8211; 59<\/td><\/tr><tr><td><strong>Second<\/strong><\/td><td><code>s<\/code><\/td><td>01 &#8211; 59<\/td><\/tr><tr><td><strong>Timestamp<\/strong><\/td><td><code>U<\/code><\/td><td>Amount of seconds since Jan 1970<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">2. Getting DateTime Object<\/h2>\n\n\n\n<p><strong>CURRENT \/ TODAY DATE<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">echo current_time('d\/m\/Y');\necho current_time('mysql'); \/\/ 'mysql' is shortcut of 'Y-m-d H:i:s'\necho current_time('mysql', true); \/\/ in GMT timezone\n\n\/\/ get DateTime object\n$current_dt = current_datetime();\necho $current_dt-&gt;format('d\/m\/Y');\n\n\/\/ if you want to use the format from Settings:\necho $current_date-&gt;format(get_option('date_format'));\n\n<\/code><\/pre>\n\n\n\n<p><strong>FROM POST ID:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$post_date = get_post_datetime($post_id);\necho $post_date-&gt;format('d\/m\/Y');<\/code><\/pre>\n\n\n\n<p><strong>CONVERT FROM STRING:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$meta_date = '13\/01\/2020';\n$datetime = DateTime::createFromFormat('d\/m\/Y', $meta_date);\n\necho $datetime-&gt;format('d M Y'); \/\/ 13 Jan 2020<\/code><\/pre>\n\n\n\n<p><strong>CONVERT FROM SPECIAL STRING:<\/strong><\/p>\n\n\n\n<p>Published date of Posts and Comments are stored in the format <code>Y-m-d H:i:s<\/code>. If the string is in that format, you can use a shortcut to convert:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$post_date = '2020-01-16 12:00:00';\n$datetime = new DateTime($post_date);\n\necho $datetime-&gt;format('d M Y'); \/\/ 16 Jan 2020<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Distance Between DateTimes<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$datetime1 = DateTime::createFromFormat('d\/m\/Y H:i', '10\/02\/2019 10:00');\n$datetime2 = DateTime::createFromFormat('d\/m\/Y H:i', '10\/02\/2020 23:00');\n\n$diff = $datetime1-&gt;diff($datetime2);\n$days_ago = $diff-&gt;days;\n$months_ago = $diff-&gt;m + ($diff-&gt;y * 12);\n$hours_ago = $diff-&gt;h + ($diff-&gt;days * 24);\n\necho \"{$days_ago} days ago\"; \/\/ 365 days ago\necho \"{$hours_ago} hours ago\"; \/\/ 8776 hours ago\necho \"{$months_ago} months ago\"; \/\/ 12 months ago<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. Modify DateTime<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$datetime = DateTime::createFromFormat('d\/m\/Y', '10\/02\/2020');\n\n$datetime-&gt;modify('+1 day');\necho $datetime-&gt;format('d M Y'); \/\/ 11 Feb 2020\n\n$datetime-&gt;modify('+2 day +1 month');\necho $datetime-&gt;format('d M Y'); \/\/ 14 Mar 2020\n\n$datetime-&gt;modify('-10 day -2 month -1 year');\necho $datetime-&gt;format('d M Y')); \/\/ 04 Jan 2019<\/code><\/pre>\n\n\n\n<p><strong>Notes<\/strong>: The DateTime you get from <code>current_datetime()<\/code> can&#8217;t be modified. So you need to workaround like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$dt = new DateTime(current_time('mysql'));\n$dt->modify('+30 day');<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-dots\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>There are other methods, but I skip them because they&#8217;re doing the same thing like <code>current_time()<\/code>.<\/p>\n\n\n\n<p>All the above should cover most use cases in a WordPress site. Let us know in the comments if we are missing some important bits \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many overlapping Date methods in PHP and WordPress. This article will show you the best way to handle Date object.<\/p>\n","protected":false},"author":1,"featured_media":1293,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[31],"class_list":["post-1279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","tag-php"],"blocksy_meta":"","_links":{"self":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/comments?post=1279"}],"version-history":[{"count":15,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1279\/revisions"}],"predecessor-version":[{"id":2270,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1279\/revisions\/2270"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media\/1293"}],"wp:attachment":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media?parent=1279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/categories?post=1279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/tags?post=1279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}