Epoch time in Perl

PERLServer Side ProgrammingProgramming Scripts

You can use the time() function in Perl to get epoch time, i.e., the numbers of seconds that have elapsed since a given date, in Unix is January 1, 1970.

Example

 Live Demo

#!/usr/local/bin/perl
$epoc = time();
print "Number of seconds since Jan 1, 1970 - $epoc\n";

Output

When the above code is executed, it produces the following result −

Number of seconds since Jan 1, 1970 - 1361022130

You can convert a given number of seconds into date and time string as follows −

Example

 Live Demo

#!/usr/local/bin/perl
$datestring = localtime();
print "Current date and time $datestring\n";
$epoc = time();
$epoc = $epoc - 24 * 60 * 60;    # one day before of current date.
$datestring = localtime($epoc);
print "Yesterday's date and time $datestring\n";

Output

When the above code is executed, it produces the following result −

Current date and time Tue Jun 5 05:54:43 2018
Yesterday's date and time Mon Jun 4 05:54:43 2018
raja
Published on 29-Nov-2019 06:33:42
Advertisements