{"id":1091,"date":"2023-08-07T13:58:07","date_gmt":"2023-08-07T08:28:07","guid":{"rendered":"https:\/\/vining.in\/blog\/?p=1091"},"modified":"2023-08-07T14:01:11","modified_gmt":"2023-08-07T08:31:11","slug":"how-to-calculate-the-difference-between-two-dates-using-php","status":"publish","type":"post","link":"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/","title":{"rendered":"How to calculate the difference between two dates using PHP?"},"content":{"rendered":"<p>Suppose you have two dates for the form:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">Start Date: 2005-03-24\r\nEnd Date: 2008-06-26<\/pre>\n<p>&nbsp;<\/p>\n<p>I suggest using DateTime and Date Interval objects.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$date1= new DateTime(\"2005-03-24\"); \r\n$date2 = new DateTime(\"2008-06-26\"); \r\n$interval = $date1-&gt;diff($date2); \r\necho \"difference \" . $interval-&gt;y . \" years, \" . $interval-&gt;m.\" months, \".$interval-&gt;d.\" days \"; \r\n\/\/ shows the total amount of days (not divided into years, months and days like above) \r\necho \"difference \" . $interval-&gt;days . \" days \";<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose you have two dates for the form: Start Date: 2005-03-24 End Date: 2008-06-26 &nbsp; I suggest using DateTime and Date Interval objects. $date1= new DateTime(&#8220;2005-03-24&#8221;); $date2 = new DateTime(&#8220;2008-06-26&#8221;); $interval = $date1-&gt;diff($date2); echo &#8220;difference &#8221; . $interval-&gt;y . &#8221; years, &#8221; . $interval-&gt;m.&#8221; months, &#8220;.$interval-&gt;d.&#8221; days &#8220;; \/\/ shows the total amount of days [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,1],"tags":[],"class_list":["post-1091","post","type-post","status-publish","format-standard","hentry","category-php","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to calculate the difference between two dates using PHP? - Programmer Code<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to calculate the difference between two dates using PHP? - Programmer Code\" \/>\n<meta property=\"og:description\" content=\"Suppose you have two dates for the form: Start Date: 2005-03-24 End Date: 2008-06-26 &nbsp; I suggest using DateTime and Date Interval objects. $date1= new DateTime(&quot;2005-03-24&quot;); $date2 = new DateTime(&quot;2008-06-26&quot;); $interval = $date1-&gt;diff($date2); echo &quot;difference &quot; . $interval-&gt;y . &quot; years, &quot; . $interval-&gt;m.&quot; months, &quot;.$interval-&gt;d.&quot; days &quot;; \/\/ shows the total amount of days [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Programmer Code\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vining.in\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-07T08:28:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-07T08:31:11+00:00\" \/>\n<meta name=\"author\" content=\"codeadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"codeadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/\",\"url\":\"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/\",\"name\":\"How to calculate the difference between two dates using PHP? - Programmer Code\",\"isPartOf\":{\"@id\":\"https:\/\/vining.in\/blog\/#website\"},\"datePublished\":\"2023-08-07T08:28:07+00:00\",\"dateModified\":\"2023-08-07T08:31:11+00:00\",\"author\":{\"@id\":\"https:\/\/vining.in\/blog\/#\/schema\/person\/cda9f07adf3479ac270e2320a914552e\"},\"breadcrumb\":{\"@id\":\"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vining.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to calculate the difference between two dates using PHP?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vining.in\/blog\/#website\",\"url\":\"https:\/\/vining.in\/blog\/\",\"name\":\"Programmer Code\",\"description\":\"- Where Developers Learn, Share, &amp; Build Careers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vining.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/vining.in\/blog\/#\/schema\/person\/cda9f07adf3479ac270e2320a914552e\",\"name\":\"codeadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vining.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/770f5bb2b95d41f98f29e7251fb1873b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/770f5bb2b95d41f98f29e7251fb1873b?s=96&d=mm&r=g\",\"caption\":\"codeadmin\"},\"url\":\"https:\/\/vining.in\/blog\/author\/codeadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to calculate the difference between two dates using PHP? - Programmer Code","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/","og_locale":"en_US","og_type":"article","og_title":"How to calculate the difference between two dates using PHP? - Programmer Code","og_description":"Suppose you have two dates for the form: Start Date: 2005-03-24 End Date: 2008-06-26 &nbsp; I suggest using DateTime and Date Interval objects. $date1= new DateTime(\"2005-03-24\"); $date2 = new DateTime(\"2008-06-26\"); $interval = $date1-&gt;diff($date2); echo \"difference \" . $interval-&gt;y . \" years, \" . $interval-&gt;m.\" months, \".$interval-&gt;d.\" days \"; \/\/ shows the total amount of days [&hellip;]","og_url":"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/","og_site_name":"Programmer Code","article_publisher":"https:\/\/www.facebook.com\/vining.in","article_published_time":"2023-08-07T08:28:07+00:00","article_modified_time":"2023-08-07T08:31:11+00:00","author":"codeadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"codeadmin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/","url":"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/","name":"How to calculate the difference between two dates using PHP? - Programmer Code","isPartOf":{"@id":"https:\/\/vining.in\/blog\/#website"},"datePublished":"2023-08-07T08:28:07+00:00","dateModified":"2023-08-07T08:31:11+00:00","author":{"@id":"https:\/\/vining.in\/blog\/#\/schema\/person\/cda9f07adf3479ac270e2320a914552e"},"breadcrumb":{"@id":"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/vining.in\/blog\/how-to-calculate-the-difference-between-two-dates-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vining.in\/blog\/"},{"@type":"ListItem","position":2,"name":"How to calculate the difference between two dates using PHP?"}]},{"@type":"WebSite","@id":"https:\/\/vining.in\/blog\/#website","url":"https:\/\/vining.in\/blog\/","name":"Programmer Code","description":"- Where Developers Learn, Share, &amp; Build Careers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vining.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/vining.in\/blog\/#\/schema\/person\/cda9f07adf3479ac270e2320a914552e","name":"codeadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vining.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/770f5bb2b95d41f98f29e7251fb1873b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/770f5bb2b95d41f98f29e7251fb1873b?s=96&d=mm&r=g","caption":"codeadmin"},"url":"https:\/\/vining.in\/blog\/author\/codeadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/posts\/1091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/comments?post=1091"}],"version-history":[{"count":1,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/posts\/1091\/revisions"}],"predecessor-version":[{"id":1092,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/posts\/1091\/revisions\/1092"}],"wp:attachment":[{"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/media?parent=1091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/categories?post=1091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/tags?post=1091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}