views:

224

answers:

3

Hi,

I'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 -

$dateObject = Zend_Date('13.04.2006', array('date_format' => 'dd.MM.yyyy'));

this line, however, throws an error - call to undefined function.

So instead I tried this -

$dt = Zend_Locale_Format::getDate('13.04.2006', array('date_format' => 'dd.MM.yyyy'));

This gets the job done, but throws an error if a date that is entered isn't valid. The docs make it look like you can use the isDate() function to check validity -

Zend_Date::isDate('13.04.2006', array('date_format' => 'dd.MM.yyyy'))

but this line always returns false.

So my questions -

Am I going about this the right way? If not, is there a better way to handle this via Zend or straight PHP?

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?

I'm locked into PHP 5.2.6 on windows, btw... so 5.3+ functions & strptime() are out.

A: 

You might want to try using the Zend_Date constants instead of strings. The reason I say this is looks like there's some inconsistency in the docs:

MM     Month, two digit                   Zend_Date::MONTH       02
MMMM   Month, localized, complete         Zend_Date::MONTH_NAME  February

and then later on:

So, if you are using 'dd.MM.yyyy' you will get '31.December.2007' but if you use 'dd.MM.YYYY' you will get '31.December.2008'

Shows MM showing a string? So your date would not validate given that format. Have you tried isDate without a format?

I've got around this issue by doing this:

try
{
    $myDate = new Zend_Date($date, $format, $locale);
}
catch (Zend_Date_Exception $e)
{
    $myDate = new Zend_Date::now();
}
Typeoneerror
+1  A: 

Take a look at this post. Maybe it will help.

Inkspeak
Thanks, this appears to be what I'm looking for as far as creating the object from the format specified. Only issue left is validating the date. If I use that method as follows -$dt = new Zend_Date('2010-05-33', 'yyyy-MM-dd');We end up with a date object containing October 31, 0038.
mld
Try $dt = new Zend_Date('2010-05-33', Zend_Date::ISO_8601); or pass the date pieces as an array.
Inkspeak
It have to be able to validate the date in the format that was specified by the input string, which could be anything. Perhaps I don't understand the isDate() function?
mld
I had to build something similar. The dates come in in a variety of formats. The user-specified "format", however, was determined by a pull-down based on the Zend_Date accepted format constants: ISO_18601, RFC_2822, TIMESTAMP, etc. I wrapped the new Zend_Date() in a try/catch in case someone put in a bad date. Works like a charm.
Inkspeak
A: 

$dateObject = Zend_Date('13.04.2006', array('date_format' => 'dd.MM.yyyy'));

throws an error "undefined function" 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

$dateObject = new Zend_Date('13.04.2006', 'dd.MM.yyyy');

and everything is cool.

ciao ulf

smoe