barrycarlyon has asked for the wisdom of the Perl Monks concerning the following question:

Given the following;

ive fetched a set of data from a database,

with a set number of columns, lets say 5

the 5th column is the timestamp columne, called timestamp.

Timestamp has the format:

0000-00-00 00:00:00
yyyy-mm-dd hr:mm:ss

How do i take this value and then convert so, that i put;

yyyy in $year
mm in $month
dd in $day

hr in $hour
mm in $minute
ss in $seconds

Hopefull that is clear enough apparently i dont make my questions clear enough so...

Yours

Replies are listed 'Best First'.
Re: conversion of a timestamp
by davidrw (Prior) on Apr 10, 2006 at 22:02 UTC
    first, don't call the column 'timestamp' -- you'll run into issues/confusion at somepoint w/that being a reserved word ..

    As for splitting up the value, you can use split or regex (perlre) or one of the many Date modules .. examples of the first two are below (i recommend error-checking on them)
    my ($y, $m, $d, $h, $min, $s) = split /[- :]/, $timestamp; my ($y, $m, $d, $h, $min, $s) = $timestamp =~ /^(\d{4})-(\d\d)-(\d\d) +(\d\d):(\d\d):(\d\d)$/; # more robust

      timestamp was just used as an example

      Barry Carlyon barry@barrycarlyon.co.uk

      how do i set $timestamp that you used?

      Barry Carlyon barry@barrycarlyon.co.uk

        You fetch it from the database; it's just an example.

Re: conversion of a timestamp
by Herkum (Parson) on Apr 11, 2006 at 02:35 UTC
    If you are pulling this from a database, I would look for a function in the database for doing the formatting. Most databases have such a function (mysql has date_format() as I recall), and it will be easier to follow in SQL rather than break it up and putting it back together in Perl.