bazz
05-31-2008, 02:55 AM
Hi,
I have a hash stored in a session, which pairs up a date with a price and, it does it correctly.
when I retrieve it, and loop the the key value pair, it shows they are matched ok.
However, when I convert the iso date to a timestamp and then, to a readable date, it changes the dates from 14th, 15th 16th, 17th, to 15th, 16th 17th and 18th. This, obviously, is wrong so I need to fix it.
ok, so I have found that the higlighted code caused the problem. Why does it shunt the dates up by one?
my $bedroom_id=$session->param('bedroom_id');
#print qq( bedroom_id = $bedroom_id ); #ok
my %dates_and_prices = % {$session->param($bedroom_id) };
use Date::Parse;
foreach my $iso_date (sort keys %dates_and_prices)
{
print "$iso_date: $dates_and_prices{$iso_date}<br />"; #ok
my $timestamp = build_timestamp($iso_date);
my $finished_date = date_conversion($timestamp);
my $price_to_two_decimal_places = sprintf("%.2f", $dates_and_prices{$iso_date});
print qq(
<tr><td>$finished_date</td><td>$price_to_two_decimal_places </td></tr>
);
}
sub build_timestamp {
my $date = shift;
use Date::Parse;
# parse and convert date to unix time for start_date
my $timestamp = str2time($date);
$timestamp += 86400;
return $timestamp;
}
sub date_conversion {
my $timestamp = shift;
return strftime( "%a %d %b %Y", localtime($timestamp) );
}
and this is what is stored in the session:
'0000000001' => {
'2008-05-15' => '98',
'2008-05-16' => '93',
'2008-05-14' => '98',
'2008-05-17' => '98'
},
bazz
I have a hash stored in a session, which pairs up a date with a price and, it does it correctly.
when I retrieve it, and loop the the key value pair, it shows they are matched ok.
However, when I convert the iso date to a timestamp and then, to a readable date, it changes the dates from 14th, 15th 16th, 17th, to 15th, 16th 17th and 18th. This, obviously, is wrong so I need to fix it.
ok, so I have found that the higlighted code caused the problem. Why does it shunt the dates up by one?
my $bedroom_id=$session->param('bedroom_id');
#print qq( bedroom_id = $bedroom_id ); #ok
my %dates_and_prices = % {$session->param($bedroom_id) };
use Date::Parse;
foreach my $iso_date (sort keys %dates_and_prices)
{
print "$iso_date: $dates_and_prices{$iso_date}<br />"; #ok
my $timestamp = build_timestamp($iso_date);
my $finished_date = date_conversion($timestamp);
my $price_to_two_decimal_places = sprintf("%.2f", $dates_and_prices{$iso_date});
print qq(
<tr><td>$finished_date</td><td>$price_to_two_decimal_places </td></tr>
);
}
sub build_timestamp {
my $date = shift;
use Date::Parse;
# parse and convert date to unix time for start_date
my $timestamp = str2time($date);
$timestamp += 86400;
return $timestamp;
}
sub date_conversion {
my $timestamp = shift;
return strftime( "%a %d %b %Y", localtime($timestamp) );
}
and this is what is stored in the session:
'0000000001' => {
'2008-05-15' => '98',
'2008-05-16' => '93',
'2008-05-14' => '98',
'2008-05-17' => '98'
},
bazz