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?
Asked
Active
Viewed 349 times
0
-
It would all depend on the database load, whether you use the tools on the DB or in the perl process. There are some good answers below for each. – alexmac Mar 01 '14 at 15:59
4 Answers
3
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.

Nishant Shrivastava
- 527
- 3
- 11
2
The best thing is to let the database do the work. See SQL statement to select all rows from previous day for an example.
-
2This would be a better response if you just added some simple SQL as an example – Neil Lunn Mar 01 '14 at 09:49
1
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";
}

Pradeep
- 3,093
- 17
- 21
-
Seems a good approach but i don't get your $ts? it is a mysql data type "timestamp', the format is(i.e.) 1970-01-31 00:00:00 – user3325861 Mar 01 '14 at 08:31
-
it worked as desired, but i had to add code to convert mysql timestap to unix timestamp – user3325861 Mar 02 '14 at 18:12
0
You can use localtime
for that.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($unix_timestamp);

Chankey Pathak
- 21,187
- 12
- 85
- 133