{"id":5155,"date":"2013-04-26T15:04:52","date_gmt":"2013-04-26T20:04:52","guid":{"rendered":"http:\/\/webirix.com\/?p=5155"},"modified":"2013-08-02T14:05:42","modified_gmt":"2013-08-02T19:05:42","slug":"como-mostrar-fechas-en-distintos-formatos-en-ingles-y-espanol-con-php","status":"publish","type":"post","link":"https:\/\/webirix.com\/en\/como-mostrar-fechas-en-distintos-formatos-en-ingles-y-espanol-con-php\/","title":{"rendered":"How to display dates in different formats in English and Spanish with PHP"},"content":{"rendered":"<p>Hello everyone, this time we are going to learn how to make a small function that will help us to show the dates in different formats and also make translations into English or Spanish.<\/p>\n<pre>&lt;?php\r\nfunction now($format, $language = \"es\") {\r\n if($format == 1) {\r\n return ($language === \"es\") ? date(\"d\/m\/y\", time()) : date(\"m\/d\/y\", time());\r\n } elseif($format == 2) { \r\n $day = date(\"l\");\r\n\r\n if ($language === \"es\") {\r\n if ($day == \"Monday\") $day = \"Lunes\";\r\n if ($day == \"Tuesday\") $day = \"Martes\";\r\n if ($day == \"Wednesday\") $day = \"Mi\u00e9rcoles\";\r\n if ($day == \"Thursday\") $day = \"Jueves\";\r\n if ($day == \"Friday\") $day = \"Viernes\";\r\n if ($day == \"Saturday\") $day = \"Sabado\";\r\n if ($day == \"Sunday\") $day = \"Domingo\";\r\n }\r\n\r\n $day2 = date(\"d\");\r\n $month = date(\"F\");\r\n\r\n if ($language === \"es\") {\r\n if ($month == \"January\") $month = \"Enero\";\r\n if ($month == \"February\") $month = \"Febrero\";\r\n if ($month == \"March\") $month = \"Marzo\";\r\n if ($month == \"April\") $month = \"Abril\";\r\n if ($month == \"May\") $month = \"Mayo\";\r\n if ($month == \"June\") $month = \"Junio\";\r\n if ($month == \"July\") $month = \"Julio\";\r\n if ($month == \"August\") $month = \"Agosto\";\r\n if ($month == \"September\") $month = \"Setiembre\";\r\n if ($month == \"October\") $month = \"Octubre\";\r\n if ($month == \"November\") $month = \"Noviembre\";\r\n if ($month == \"December\") $month = \"Diciembre\";\r\n }\r\n\r\n $year = date(\"Y\"); \r\n\r\n return ($language === \"es\") ? \"$day, $day2 de $month de $year\" : \"$month $day2, $year\";\r\n } elseif($format == 3) {\r\n return ($language === \"es\") ? date(\"d-m-Y H:i:s\", time()) : date(\"Y-m-d H:i:s\", time());\r\n }\r\n}\r\n?&gt;\r\n\r\n<span style=\"font-family: arial, helvetica, sans-serif;\">To use it we can simply send it to call in the following way:\r\n<span style=\"font-family: Consolas, Monaco, monospace;\">\r\n&lt;?php<\/span><\/span><\/pre>\n<pre> echo now (1); \/\/ Print: 03\/27\/13 echo now (1, &quot;en&quot;); \/\/ Print: 03\/27\/13 echo now (2); \/\/ Print: Wednesday, March 27, 2013 echo now (2, &quot;en&quot;); \/\/ Print: March 27, 2013 echo now (3); \/\/ Print: 2013-03-27 12:00:00 echo now (3, &quot;en&quot;); \/\/ Print: 2013-03-27 12:00:00\r\n\r\n<span style=\"font-family: arial, helvetica, sans-serif;\">I hope this is useful for you, regards!<\/span><\/pre>\n<pre><\/pre>","protected":false},"excerpt":{"rendered":"<p>Hola a todos en esta ocasi\u00f3n vamos a aprender c\u00f3mo hacer una peque\u00f1a funci\u00f3n que nos servir\u00e1 para mostrar las fechas en distintos formatos y adem\u00e1s hacerles traducciones al Ingl\u00e9s o al Espa\u00f1ol. &lt;?php function now($format, $language = &#8220;es&#8221;) { if($format == 1) { return ($language === &#8220;es&#8221;) ? date(&#8220;d\/m\/y&#8221;, time()) : date(&#8220;m\/d\/y&#8221;, time()); } &#8230;<\/p>","protected":false},"author":9,"featured_media":5179,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2014],"tags":[],"class_list":["post-5155","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programacionweb"],"jetpack_featured_media_url":"https:\/\/webirix.com\/wp-content\/uploads\/2013\/04\/codigo-php.png","_links":{"self":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/5155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/comments?post=5155"}],"version-history":[{"count":5,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/5155\/revisions"}],"predecessor-version":[{"id":7268,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/5155\/revisions\/7268"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/media\/5179"}],"wp:attachment":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/media?parent=5155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/categories?post=5155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/tags?post=5155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}