PEAR Date library - creating a new Date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bostonblackie
Forum Newbie
Posts: 2
Joined: Wed Mar 04, 2009 3:00 pm

PEAR Date library - creating a new Date

Post by bostonblackie »

I've run into an issue with the PEAR Date library that is mystifying me. I've used Date for a while now, and it can get a little tricky, but this is beyond me.

I'm using PHP v4.3.9
Using the PEAR Date library

$_REQUEST holds three variables (comes from a Javascript calendar) that define the date. I turn them into one string via sprintf, then get a new Date object.

Code: Select all

$x = sprintf("%s-%s-%s", $_REQUEST['y'], $_REQUEST['m'], $_REQUEST['d'] )  ;
$targetDate = new Date( $x ) ;  
However, the newly created Date object is blank, even though $x is a String type and does print out properly, like "2007-01-01".

What is baffling me is that if I simply say...

Code: Select all

$x = "2007-01-01";
$targetDate = new Date( $x ) ;
... in this case the $targetDate object is created properly. I just can't seem to find a way that $x in the first example is different from $x in the second. I've looked at whether $x is being typed correctly and it is.

Thank you for any assistance. I've looked around for information that would help me but I find I'm just beating my head against the wall.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PEAR Date library - creating a new Date

Post by requinix »

What does

Code: Select all

echo bin2hex($x);
give?

And have you tried creating a default Date object and calling setDate manually?
bostonblackie
Forum Newbie
Posts: 2
Joined: Wed Mar 04, 2009 3:00 pm

Re: PEAR Date library - creating a new Date

Post by bostonblackie »

That worked!

bin2hex ($x) yields 323030372d30352d3233

By creating a default new date, then using setDate( date ( $x ) ) I did get a good Date object. It's not clear to me why the other method wasn't working, but this one does.

Thank you very much.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PEAR Date library - creating a new Date

Post by requinix »

I think the constructor expects the date/time to be in a specific format while setDate is a little more forgiving.
Post Reply