{"id":779,"date":"2020-10-29T10:59:01","date_gmt":"2020-10-29T10:59:01","guid":{"rendered":"https:\/\/www.onfocus.co.uk\/?p=779"},"modified":"2020-10-29T10:59:03","modified_gmt":"2020-10-29T10:59:03","slug":"use-php-to-format-date-from-mysql","status":"publish","type":"post","link":"https:\/\/www.onfocus.co.uk\/blog\/use-php-to-format-date-from-mysql\/","title":{"rendered":"Use PHP to format date from MySQL"},"content":{"rendered":"\n<p>I needed to display a date correctly on a web page that was retrieved from a MySQL database.<\/p>\n\n\n\n<p>Although the date was inserted into the database with a British format of dd\/mm\/yyyy, it is getting stored as a timestamp within the SQL.<\/p>\n\n\n\n<p>So when retrieving the date later it would show as <code>mm\/dd\/yy<\/code>. This was in an American format and not what I required.<\/p>\n\n\n\n<p>What I needed to do was retrieve the timestamp from the database and use php to convert \/ format the date accordingly for my purpose.<\/p>\n\n\n\n<p>Using a combination of the <em>date() and strtotime()<\/em> functions as follows I was able to format the date to my requirements:<\/p>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p>date( &#8220;d\/m\/y&#8221;, strtotime($timestamp));<\/p><\/blockquote><\/figure>\n\n\n\n<p>The date() function can be used to change the format with various recognised characters in the parameter string. For my needs the popular ones were:<\/p>\n\n\n\n<p><strong>d<\/strong> = Day of the month (2 digits with leading zeros 01 to 31)<br><strong>D<\/strong> = Day of the month (three letters Mon through Sun)<br><strong>m<\/strong> = Month (leading zeros 01 through 12)<br><strong>M<\/strong> = Month (three letters Jan through Dec)<br><strong>Y<\/strong> = Year (4 digits 1999 or 2020)<br><strong>y<\/strong> = Year (2 digits 99, 01 or 20)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I needed to display a date correctly on a web page that was retrieved from a MySQL database. Although the date was inserted into the database with a British format of dd\/mm\/yyyy, it is&#46;&#46;&#46;<\/p>\n","protected":false},"author":4,"featured_media":780,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[50,48],"class_list":["post-779","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-website-design","tag-mysql","tag-php"],"_links":{"self":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts\/779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=779"}],"version-history":[{"count":1,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts\/779\/revisions"}],"predecessor-version":[{"id":781,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts\/779\/revisions\/781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/media\/780"}],"wp:attachment":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}