473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Converting a date/timestamp

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.
Jul 6 '07 #1
7 12931
numberwhun
3,509 Expert Mod 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. ##########################################################################
  2. #  Date information for time stamps
  3. ##########################################################################
  4. my $dtstamp;
  5. my @months = qw(01 02 03 04 05 06 07 08 09 10 11 12);
  6. my @weekDays = qw(01 02 03 04 05 06 07);
  7. (my $second, my $minute, my $hour, my $dayOfMonth, my $month, my $yearOffset, my $dayOfWeek, my $dayOfYear, my $daylightSavings) = localtime();
  8. my $year = 1900 + $yearOffset;
  9. my $theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
  10.  
  11. if ( $dayOfMonth > 9 )
  12.    my $dt = $months[$month] . "/" . $dayOfMonth . "/" . $year; 
  13.    $dtstamp = $year . $months[$month] . $dayOfMonth;
  14.    $datetime = $year . $months[$month] . $dayOfMonth . "." . $hour . $minute . $second;
  15. }
  16. else
  17. {
  18.    my $dt = $months[$month] . "/" . '0'.$dayOfMonth . "/" . $year; 
  19.    $dtstamp = $year . $months[$month] . '0'.$dayOfMonth;
  20.    $datetime = $year . $months[$month] . '0'.$dayOfMonth . "." . $hour . $minute . $second;
  21. }
  22.  
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
Jul 6 '07 #2
KevinADC
4,059 Expert 2GB
Look into using Time::Local

perldoc Time::Local

if you get stuck, ask more questions.
Jul 6 '07 #3
miller
1,089 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1. my $sth = $conn->prepare(q{SELECT DATE_FORMAT(date, "%a, %e %b %Y %H:%i:%S"), title, summary FROM news});
  2. $sth->execute() or die $conn->errstr();
  3.  
- Miller
Jul 6 '07 #4
KevinADC
4,059 Expert 2GB
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.
Jul 6 '07 #5
numberwhun
3,509 Expert Mod 2GB
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
Jul 6 '07 #6
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();
Jul 7 '07 #7
KevinADC
4,059 Expert 2GB
Very good, thanks for the follow up.

Kevin
Jul 7 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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?
0
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...
1
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
6
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...
3
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",...
2
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 .........
6
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!
2
by: Yash3 | last post by:
Hey I am trying this create or replace FUNCTION DatesPast ( v_dueDate Timestamp ) RETURN NUMBER AS DaysPast DATE BEGIN
5
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...
1
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...
0
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...
0
isladogs
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...
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.