#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    USA
    Posts
    67
    Rep Power
    15
    hello
    i am trying to make a log table where i insert the date into the table. how can i grab the current date in a yyyy-mm-dd format?

    thanks for the help
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    Hi David,


    you can use this following script for getting the date in yyyy-mm-dd format.

    just use &date_format for passing this date format to the database.


    #!/usr/bin/perl

    ####-----Create yyyy-mm-dd date format---####

    print &date_format;


    sub date_format{

    ($sec,$min,$hour,$wday,$month,$year,$mday)=localtime(time());

    $month="0".$month if ($month)<10;
    #make month in 2 digits if it is less than 2

    $wday="0".$wday if ($wday)<10;
    #make date in 2 digits if it is less than 2


    $yr=substr($year,1);
    $yr="20".$yr if length($yr)==2;

    #make year in 4 digits


    $date=$yr."-".$month."-".$wday;
    #concatenate the date in yyyy-mm-dd format.

    return $date;
    }




    ------------------
    SR -
    shiju.dreamcenter.net

    "The fear of the LORD is the beginning of knowledge..."
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    16
    Rep Power
    0
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by bydavid:
    hello
    i am trying to make a log table where i insert the date into the table. how can i grab the current date in a yyyy-mm-dd format?
    [/quote]
    I'm a lazy guy so I use modules. Here is an interesting way:

    use strict;
    use Date::Calc qw(Today Add_Delta_Days);

    print mysql_date(-1);
    print mysql_date();
    print mysql_date(+1);
    print mysql_date(+2);

    sub mysql_date {
    my $offset = shift &#0124; &#0124; 0;
    my @today = Today();
    my @date = Add_Delta_Days(@today,$offset);
    my $year = sprintf("%4d", $date[0]);
    my $month = sprintf("%02d", $date[1]);
    my $day = sprintf("%02d", $date[2]);
    my $mysql_date = qq|$year-$month-$dayn|;
    return $mysql_date;
    }



IMN logo majestic logo threadwatch logo seochat tools logo