If you need to search for specific dates or date ranges, the database engine is better equipped than bare Perl.
SELECT your_fields FROM your_table WHERE Timestamp_field BETWEEN "2003-02-01 10:30" AND "2003-02-05 12:00"
Moreover, MySQL has a large collection of date-oriented functions, which you should consider before burdening your client with tasks that should belong in the server.

However, if you really must, you can extract from a timestamp value the same fields that you get from localtime.
#!/usr/bin/perl -w use strict; my ($sec,$min,$hour,$mday,$mon,$year) = localtime(1044436437); # Wed Feb 5 10:13:57 2003 $year += 1900; $mon++; my $timestamp = "20030205101357"; my ($tyear, $tmon, $tmday, $thour,$tmin, $tsec) = $timestamp =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/; printf " Time Timestamp\n"; printf "year %4d %4d\n", $year, $tyear; printf "month %4d %4d\n", $mon, $tmon; printf "day %4d %4d\n", $mday, $tmday; printf "hour %4d %4d\n", $hour, $thour; printf "min %4d %4d\n", $min, $tmin; printf "sec %4d %4d\n", $sec, $tsec;
Having done that, you can easily compare fields coming from localtime with the ones coming from timestamps.

For complicated operations, you may want to trust some modules, such as Date::Calc or Date::Manip
_ _ _ _ (_|| | |(_|>< _|

In reply to Re: Perl's time and MySQL's timestamp by gmax
in thread Perl's time and MySQL's timestamp by powerhouse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.