Gossamer Forum
Home : General : Perl Programming :

formating the Date

Quote Reply
formating the Date
Here is my code the main part works great but the created values from SQL are in the format of

YYYY-MM-DD TT:HH:SS eg

2004-05-07 18:00:48

but the code is for a RSS feed and it states that you must print your time stamps in
RFC822 dates as per the RSS
notes.
http://feeds.archive.org/validator/...FC2822Date.html

ddd mm yyyy hh:mm:ss zzz

Wed, 19 May 2004 10:08:00 PST

I was given the code to do it but I just dont know how to add it to my script the code that does not work is in red if you remove this extra code it will work just find apart from the dates from the sql database are in the wrong format.

What I am trying to do is before I print the "created" field from SQL to the termplate I need to format it to the ddd mm yyyy hh:mm:ss zzz format and thats where I am stuck as I just cant bind this created value to a scalar reference, and then print it in to the template.

So any help on fixing this little task in the script I would be most greatfull.



Code:
#!/usr/bin/perl use strict; use warnings; use DBI; use CGI; use CGI::Carp qw(fatalsToBrowser); use HTML::Template; use Time::ParseDate; use Date::Format; my $query = CGI->new(); ## Define your username/password/SQL server my $db_server = "localhost"; my $db_user = "videodb"; my $db_password = "password"; my $db_database = "VideoDB"; ## get the current time and date my($date,$min,$hour,$day,$month,$year,$weekday,$ampm,$zero,@weekdays,@months); (undef, $min, $hour, $day, $month, $year, $weekday) = localtime(time); @weekdays = ("Sun","Mon","Tue","Wed", "Thu","Fri","Sat"); @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul", "Aug","Sep","Oct","Nov","Dec"); # Dates correct through 2097 :) if ($year > 97) { $year += 1900; } else { $year += 2000; } # Fix Time if ($hour > 12) { $hour -= 12; $ampm = "pm"; } else { $ampm = "am"; } if ($hour == 0) { $hour = 12; } if ($min < 10) { $zero = "0"; } else { $zero = ""; } # Get the current time and format it to the RFC822 format my $timeanddate="$weekdays[$weekday], $day $months[$month] $year $hour:$zero$min:00 PST"; # Connect to the database and throw a error if I cant connect my $dbh = DBI->connect("dbi:mysql:$db_database:$db_server", $db_user,$db_password,{ RaiseError => 1}) or die $DBI::errstr; ## Create SQL calls # Query 1 to get desired records for the loop my $last_five = q~SELECT imdbID, title, Mediatype, created, plot FROM videodata WHERE Mediatype <> '50' ORDER BY created DESC LIMIT 5~; # Query 2 to get the total records in DB my $total = q~SELECT COUNT(*) FROM videodata~; # Bind the assign vaules to scalar reference so we can attemp to cleanup the created date my ($imdbID,$title,$MediaType,$created, $plot); $last_five->bind_columns(\($imdbID,$title,$MediaType,$created, $plot)); ## Define Template ## my $tmpl = HTML::Template->new( filename => "sig.tmpl"); my $tmpl = HTML::Template->new(filename => 'sig2.tmpl', die_on_bad_params => 0); ## Define how we want the time and date formated in the RFC822 format my $timetemp = '%a, %d %b %Y %T %Z'; my $fmtime = time2str($timetemp, parsedate($created)); ## Feed the information you got from the query (@rows) into your template $tmpl->param(rows => $dbh->selectall_arrayref($last_five, { Slice => {} }), count => ($dbh->selectrow_array($total))[0]); ## Add the current time and date to the template $tmpl->param(timeanddate => $timeanddate); ## Print the template print $tmpl->output;
Quote Reply
Re: [SimonTOZ] formating the Date In reply to
Sorry not sure why the code came out as one long line :-( I will try again


Code:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser)
;
use HTML::Template;
use Time::ParseDate;
use Date::Format;
my $query = CGI->new();
## Define your username/password/SQL server
my $db_server = "localhost"
; my $db_user = "videodb"; my $db_password = "password"; my $db_database = "VideoDB";

## get the current time and date
my($date,$min,$hour,$day,$month,$year,$weekday,$ampm,$zero,@weekdays,@months);
(undef, $min, $hour, $day, $month, $year, $weekday) = localtime(time); @weekdays = ("Sun","Mon","Tue","Wed", "Thu","Fri","Sat"); @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul", "Aug","Sep","Oct","Nov","Dec");
# Dates correct through 2097 :)
if ($year > 97) { $year += 1900; } else { $year += 2000; }
# Fix Time
if ($hour > 12) { $hour -= 12; $ampm = "pm"
; } else { $ampm = "am"; } if ($hour == 0) { $hour = 12; } if ($min < 10) { $zero = "0"; } else { $zero = ""; }
# Get the current time and format it to the RFC822 format
my $timeanddate="$weekdays[$weekday], $day $months[$month] $year $hour:$zero$min:00 PST"
; # Connect to the database and throw a error if I cant connect
my $dbh = DBI->connect("dbi:mysql:$db_database:$db_server"
, $db_user,$db_password,{ RaiseError => 1}) or die $DBI::errstr;
## Create SQL calls # Query 1 to get desired records for the loop
my $last_five =
q~SELECT imdbID, title, Mediatype, created, plot FROM videodata WHERE Mediatype <> '50' ORDER BY created DESC LIMIT 5~;
# Query 2 to get the total records in DB
my $total = q~SELECT COUNT(*) FROM videodata~
;

# Bind the assign vaules to
scalar reference so we can attemp to cleanup the created date
my ($imdbID,$title,$MediaType,$created, $plot); $last_five->bind_columns(\($imdbID,$title,$MediaType,$created, $plot));
## Define Template
my $tmpl = HTML::Template->new(filename => 'sig2.tmpl'
, die_on_bad_params => 0);
## Define how we want the time and date formated in the RFC822 format
my $timetemp = '%a, %d %b %Y %T %Z'
; my $fmtime = time2str($timetemp, parsedate($created));
## Feed the information you got from the query (@rows) into your template
$tmpl->param(rows => $dbh->selectall_arrayref($last_five, { Slice => {} }), count => ($dbh->selectrow_array($total))[0]);
## Add the current time and date to the template
$tmpl->param(timeanddate => $timeanddate);
## Print the template
print $tmpl->output;