Re: getting a string result from Date
| From: | Demian Turner | Date: | Fri, 02 Apr 2004 08:44:49 +0000 |
| Subject: | Re: getting a string result from Date | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-11583@lists.php.net to get a copy of this message | ||
Hi Lorenzo
please give the code a try, i was baffled to find the second $currdate assignment doesn't work when it's fed back into the argument of the Date object in the next iteration of the loop.
using $currDate = $date->format('%Y-%m-%d');
C:\htdocs\test>php dateTest.php
string(10) "2004-04-01"
string(10) "2004-03-25"
string(10) "2004-03-18"
string(10) "2004-03-11"
string(10) "2004-03-04"
using $currDate = "{$date->year}-{$date->month}-{$date->day}";
C:\htdocs\test>php dateTest.php
string(8) "2004-4-1"
string(8) "2004-4-1"
string(8) "2004-4-1"
string(8) "2004-4-1"
string(8) "2004-4-1"
the second one works, but you don't have the date returned in the ISO-8601 format because the numbers are not 0-padded.my tests show passing ISO or non-ISO formatted dates as args to the Date object make no difference - both work. this bug took me ages to find because the malfunctioning is unexpected (i can post the code again if needed). cheers Demian Lorenzo Alberton wrote:
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