{"id":1277,"date":"2022-08-30T15:15:11","date_gmt":"2022-08-30T15:15:11","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/php-zend-custom-date-input-formats-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:11","modified_gmt":"2022-08-30T15:15:11","slug":"php-zend-custom-date-input-formats-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/php-zend-custom-date-input-formats-collection-of-common-programming-errors\/","title":{"rendered":"PHP \/ Zend custom date input formats-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m creating a library component for other developers on my team using PHP and Zend. This component needs to be able to take as input a date (string) and another string telling it the format of that date. Looking through the Zend documentation and examples, I thought I found the solution &#8211;<\/p>\n<pre><code>$dateObject = Zend_Date('13.04.2006', array('date_format' =&gt; 'dd.MM.yyyy'));\n<\/code><\/pre>\n<p>this line, however, throws an error &#8211; call to undefined function.<\/p>\n<p>So instead I tried this &#8211;<\/p>\n<pre><code>$dt = Zend_Locale_Format::getDate('13.04.2006', array('date_format' =&gt; 'dd.MM.yyyy'));\n<\/code><\/pre>\n<p>This gets the job done, but throws an error if a date that is entered isn&#8217;t valid. The docs make it look like you can use the isDate() function to check validity &#8211;<\/p>\n<pre><code>Zend_Date::isDate('13.04.2006', array('date_format' =&gt; 'dd.MM.yyyy'))\n<\/code><\/pre>\n<p>but this line always returns false.<\/p>\n<p>So my questions &#8211;<\/p>\n<p>Am I going about this the right way? If not, is there a better way to handle this via Zend or straight PHP?<\/p>\n<p>If I do use Zend_Locale_Format::getDate(), do I need to worry about a locale being set elsewhere and changing the results of the call?<\/p>\n<p>I&#8217;m locked into PHP 5.2.6 on windows, btw&#8230; so 5.3+ functions &amp; strptime() are out.<\/p>\n<ol>\n<li>\n<p>Take a look at this post. Maybe it will help.<\/p>\n<\/li>\n<li>\n<p>You might want to try using the Zend_Date constants instead of strings. The reason I say this is looks like there&#8217;s some inconsistency in the docs:<\/p>\n<pre>\nMM     Month, two digit                   Zend_Date::MONTH       02\nMMMM   Month, localized, complete         Zend_Date::MONTH_NAME  February\n<\/pre>\n<p>and then later on:<\/p>\n<blockquote>\n<p>So, if you are using &#8216;dd.MM.yyyy&#8217; you will get &#8217;31.<strong>December<\/strong>.2007&#8242; but if you use &#8216;dd.MM.YYYY&#8217; you will get &#8217;31.December.2008&#8242;<\/p>\n<\/blockquote>\n<p>Shows MM showing a string? So your date would not validate given that format. Have you tried isDate without a format?<\/p>\n<p>I&#8217;ve got around this issue by doing this:<\/p>\n<pre><code>try\n{\n    $myDate = new Zend_Date($date, $format, $locale);\n}\ncatch (Zend_Date_Exception $e)\n{\n    $myDate = new Zend_Date::now();\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<blockquote>\n<p>$dateObject = Zend_Date(&#8216;13.04.2006&#8217;, array(&#8216;date_format&#8217; =&gt; &#8216;dd.MM.yyyy&#8217;));<\/p>\n<\/blockquote>\n<p>throws an error &#8220;undefined function&#8221; because it is no function. accordingly to the manual Zend_Date is an Object with a constructor and the format string is not given in an array. you have to write it like this<\/p>\n<blockquote>\n<p>$dateObject = new Zend_Date(&#8216;13.04.2006&#8217;, &#8216;dd.MM.yyyy&#8217;);<\/p>\n<\/blockquote>\n<p>and everything is cool.<\/p>\n<p>ciao ulf<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:13:54. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m creating a library component for other developers on my team using PHP and Zend. This component needs to be able to take as input a date (string) and another string telling it the format of that date. Looking through the Zend documentation and examples, I thought I found the solution &#8211; $dateObject = Zend_Date(&#8216;13.04.2006&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1277","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1277"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1277\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}