Re: getting a string result from Date
| From: | Lorenzo Alberton | Date: | Thu, 01 Apr 2004 17:47:56 +0000 |
| Subject: | Re: getting a string result from Date | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-11571@lists.php.net to get a copy of this message | ||
Hi Demian
On Thu, 01 Apr 2004 17:24:29 +0100, Demian Turner wrote:
> However I have come across one hitch which I've solved but don't
> understand the solution. Does any understand why the first method
> of assigning the results of a date span operation to a string works
> and not the (commented) second?
>
> $currDate = $date->format('%Y-%m-%d');
> #$currDate = "{$date->year}-{$date->month}-{$date->day}";
the second one works, but you don't have the date returned
in the ISO-8601 format because the numbers are not 0-padded.
For instance, you get "2004-4-1" instead of "2004-04-01".
You should do something like
$currDate = sprintf("%04d-%02d%02d",
$date->year,
$date->month,
$date->day);
but of course $currDate = $date->format('%Y-%m-%d');
is much shorter and clearer :-)
HTH
Lorenzo