I never used Perl before, so I need a little help formating a date.
What I'm doing is querying a postgres database and then creating a .xml file for a RSS feed. I don't know how to correctly format the timestamp for my .xml file.
Let say I query the database and then store the values in variables. I then print the variables to an .xml file. Here is an example:
my $sth = $conn->prepare("SELECT date,title,summary From news");
$sth->execute() or die "execute failed: " . $sth->errstr();
my($date, $title,$sum);
while (($date,$title,$sum) = $sth->fetchrow()) {
print XML " <lastBuildDate>$date</lastBuildDate>\n";
}
How do I create the correct format that I need when I print $date?
The format of the timestamp in $date that I get from the database is:
2007-07-02 11:30:01.71055
I need to reformat it to look like:
Tue, 19 Dec 2006 20:15:00 MST
Could someone show me how to do this? I've search on-line for a few hours with no luck.
7 12931
First, Perl is an awesome language and in my opinion, the best there is. If you are going to be coding in Perl, I recommend you pick up "Learning Perl" from O'Reilly. The latest one is 4th edition and is authored by Randal Schwartz, Tom Phoenix, and Brian D Foy.
As far as date time stamps, I have not delved into gathering them from a database. Instead, I use the following code that allows me the freedom to format it the way I wish. Feel free to modify it to meet your needs. -
##########################################################################
-
# Date information for time stamps
-
##########################################################################
-
my $dtstamp;
-
my @months = qw(01 02 03 04 05 06 07 08 09 10 11 12);
-
my @weekDays = qw(01 02 03 04 05 06 07);
-
(my $second, my $minute, my $hour, my $dayOfMonth, my $month, my $yearOffset, my $dayOfWeek, my $dayOfYear, my $daylightSavings) = localtime();
-
my $year = 1900 + $yearOffset;
-
my $theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
-
-
if ( $dayOfMonth > 9 )
-
{
-
my $dt = $months[$month] . "/" . $dayOfMonth . "/" . $year;
-
$dtstamp = $year . $months[$month] . $dayOfMonth;
-
$datetime = $year . $months[$month] . $dayOfMonth . "." . $hour . $minute . $second;
-
}
-
else
-
{
-
my $dt = $months[$month] . "/" . '0'.$dayOfMonth . "/" . $year;
-
$dtstamp = $year . $months[$month] . '0'.$dayOfMonth;
-
$datetime = $year . $months[$month] . '0'.$dayOfMonth . "." . $hour . $minute . $second;
-
}
-
For more information on date time stamping, see the following perldoc reference page: http://perldoc.perl.org/functions/localtime.html
I hope that this helps.
Regards,
Jeff
How do I create the correct format that I need when I print $date?
The format of the timestamp in $date that I get from the database is:
2007-07-02 11:30:01.71055
I need to reformat it to look like:
Tue, 19 Dec 2006 20:15:00 MST
Could someone show me how to do this? I've search on-line for a few hours with no luck.
What database are you using? I would suggest that in this circumstance that you simply let the database do the formatting for you. Here is a link to the documentation for MySQL 5.1 mysql5.1 Ref Man: Date and Time Functions
Changing your code to the following: -
my $sth = $conn->prepare(q{SELECT DATE_FORMAT(date, "%a, %e %b %Y %H:%i:%S"), title, summary FROM news});
-
$sth->execute() or die $conn->errstr();
-
- Miller
What database are you using? I would suggest that in this circumstance that you simply let the database do the formatting for you. Here is a link to the documentation for MySQL 5.1
- Miller
That is a really good suggestion. I need to learn more about databases.
That is a really good suggestion. I need to learn more about databases.
You and me both. I know how to form SQL statements, that's easy. But have must really sit down and learn MySQL.
Regards,
Jeff
Thanks for all the help everyone.
I tried Numberwhun nice perl conversion. Great script, but not if you need to query the database. I am using it everytime I recreate my .xml file in the pubdate field.
Time::Local was great too, but not for my situtation.
I ended up changing my database query to convert the timestamp to a char. He is the query if anyone cares. This is on a postgres database.
my $sth = $conn->prepare("SELECT newsid,to_char(date, 'FMDy, DD Mon YYYY HH24:MI:SS'),title,summary FROM news ORDER BY newsid DESC")
or die "prepare failed: " . $conn->errstr();
Very good, thanks for the follow up.
Kevin
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kit |
last post by:
Howdy!
I have some dates in a MySQL database that are in a 'Y-m-d' format.
Is there a way to have PHP read these dates and convert them to a
'd-M-Y' format?
|
by: Andy Turner |
last post by:
I've just been trying to do this. I looked on Google and it seems to
be a common problem with no obvious solution. I've seen various
solutions which don't seem exactly elegant, so I figured I'd...
|
by: news.microsoft.com |
last post by:
In vb.net, is there anyway to convert a SQL timestamp field into a real
date. Also can I convert a real date into a SQL timestamp
|
by: Kentor |
last post by:
does anyone know of a script that would produce a calendar starting
from todays date with checkboxes next to every day of the month so that
the user is able to check it in case of an event for that...
|
by: Tgone |
last post by:
Hello,
I need to convert a MySQL date "2007-03-08", into "March 8th, 2007".
Normally I would use MySQL to accomplish this task but I have to use
PHP.
Here's my code:
echo date("F jS, Y",...
|
by: shenanwei |
last post by:
DB2 V8.2 on AIX, type II index is created.
I see this from deadlock event monitor.
5) Deadlocked Connection ...
Participant no.: 2
Lock wait start time: 09/18/2006 23:04:09.911774
.........
|
by: marc |
last post by:
hi
im trying to convert Date() into a unix timestamp so i can stick the
result into a mysql db, please help!
|
by: Yash3 |
last post by:
Hey
I am trying this
create or replace FUNCTION DatesPast
(
v_dueDate Timestamp
)
RETURN NUMBER
AS
DaysPast DATE
BEGIN
|
by: Hemant Shah |
last post by:
Folks,
How can I convert date/time/timestamp to an integer?
According to UDB 8.1 docs that I have,
INTEGER('1964-07-20') should return 19640720, but when I run the SQL
statement I get...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |