Domanda

I have a timestamp field on a mysql table , i want to know if that timestamp is 24hrs old or more. What would be the best way to do that in Perl?

È stato utile?

Soluzione 3

By timestamp I am assuming it's Unix timestamp, i.e. seconds since epoch.

my $ts       = 1393662619;
my $day_24hr = 24 * 60 * 60;    ## seconds in 24 hrs

my $prev_time = time() - $day_24hr;    ## 24hours ago

if ( $ts < $prev_time ) {
    print "timestamp is 24 hour old";
}

Altri suggerimenti

SQL that would return the timestamp 24 hours ago.

SELECT UNIX_TIMESTAMP(NOW() - INTERVAL 24 HOUR) 

Now if your timestamp is < the timestamp returned by the above SQL , 24 hours have passed.

The best thing is to let the database do the work. See SQL statement to select all rows from previous day for an example.

You can use localtime for that.

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($unix_timestamp);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top