{"id":14,"date":"2008-03-26T20:24:06","date_gmt":"2008-03-27T03:24:06","guid":{"rendered":"http:\/\/shanelabs.com\/blog2\/?p=14"},"modified":"2011-12-15T21:43:40","modified_gmt":"2011-12-16T05:43:40","slug":"calculating-age-from-unix-timestamps-in-php","status":"publish","type":"post","link":"https:\/\/shanelabs.com\/blog\/2008\/03\/26\/calculating-age-from-unix-timestamps-in-php\/","title":{"rendered":"Calculating age from unix timestamps in PHP"},"content":{"rendered":"<p>I couldn\u2019t find a function already created for PHP that calculates someone\u2019s age based on their birthdate and the current date (in unix timestamp format &#8211; seconds since 1970). First I tried this:<\/p>\n<pre>$age = date(\"Y\",$endtime) - date(\"Y\",$starttime);<\/pre>\n<p>But then I realized that this might not be completely accurate, base on the calendar, leap year, etc. So here\u2019s what I ended up with:<\/p>\n<pre>function computeAge($starttime,$endtime)\r\n{\r\n\t$age = date(\"Y\",$endtime) - date(\"Y\",$starttime);\r\n\t\/\/if birthday didn't occur that last year, then decrement\r\n\tif(date(\"z\",$endtime) &lt; date(\"z\",$starttime)) $age--;\r\n\treturn $age;\r\n}<\/pre>\n<p>Let me know if you have any improvements!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I couldn\u2019t find a function already created for PHP that calculates someone\u2019s age based on their birthdate and the current date (in unix timestamp format &#8211; seconds since 1970). First I tried this: $age = date(&#8220;Y&#8221;,$endtime) &#8211; date(&#8220;Y&#8221;,$starttime); But then I realized that this might not be completely accurate, base on the calendar, leap year, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-software"],"_links":{"self":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":5,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":16,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions\/16"}],"wp:attachment":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}