{"id":619,"date":"2021-11-11T22:15:36","date_gmt":"2021-11-12T03:15:36","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=619"},"modified":"2024-02-08T08:45:43","modified_gmt":"2024-02-08T13:45:43","slug":"converting-string-to-date-in-php","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/converting-string-to-date-in-php\/","title":{"rendered":"Converting string to date in PHP"},"content":{"rendered":"\n<p>Converting strings to dates in PHP requires the use of two functions. strtotime function converts the string to an epoch timestamp. date function converts the timestamp to the desirable date format.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n   $string = \"Nov 11, 2011\";\n   $timestamp = strtotime($string);\n   $date = date(\"Y-m-d\", $timestamp);\n   print $timestamp . \"&lt;br>\" . $date;\n?><\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1320987600\n2011-11-11<\/code><\/pre>\n\n\n\n<p>Y prints the date in 4 digit format, m prints the month in two digit format, and d prints the day in two digit format. Alternately, you can use the single line version of the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print date(\"Y-m-d\", strtotime(\"Nov 11, 2011\"));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Converting strings to dates in PHP requires the use of two functions. strtotime function converts the string to an epoch timestamp. date function converts the timestamp to the desirable date format. output Y prints the date in 4 digit format, m prints the month in two digit format, and d prints the day in two [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-619","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=619"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/619\/revisions"}],"predecessor-version":[{"id":620,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/619\/revisions\/620"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}