Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Datetime to Unix Timestamp in Perl

How to convert Datetime to Unix timestamp in Perl

🎈 🎈 🎈
+1

    use Time::Local;

    my ($sec, $min, $hour, $day, $month, $year) = (49, 29, 20, 31, 1-1, 2019-1900);
    my $unix_timestamp = timelocal($sec, $min, $hour, $day, $month, $year);

    print "Unix Timestamp: $unix_timestamp\n";
        

Output:
Unix Timestamp: 1548966589
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this example, Perl's timelocal() function is used to convert a specific date and time (eg 2019-01-31 20:29:49) into a Unix timestamp.
The values for year, month, day, hour, minute, and second are passed into timelocal(), with the month and year adjusted (months in Perl are 0-based, and the year is the number of years since 1900).
The resulting Unix timestamp, in this case '1548966589', is then printed.



Other useful Perl date functions

Function Description Example
time() Returns the current Unix timestamp (seconds since January 1, 1970). time() returns 1693227296
localtime() Returns the local date and time based on the Unix timestamp or the current time if no timestamp is provided. localtime(time()) returns Tue Aug 28 12:34:56 2024
gmtime() Returns the UTC date and time based on the Unix timestamp or the current time if no timestamp is provided. gmtime(time()) returns Tue Aug 28 12:34:56 2024
strftime() Formats a date and time according to the specified format using the POSIX module. strftime("%Y-%m-%d %H:%M:%S", localtime(time())) returns 2024-08-28 12:34:56
timelocal() Converts a local date and time to a Unix timestamp using the Time::Local module. timelocal(0, 34, 12, 28, 7, 2024) returns 1693227296
timegm() Converts a UTC date and time to a Unix timestamp using the Time::Local module. timegm(0, 34, 12, 28, 7, 2024) returns 1693227296
DateTime->now() Returns the current date and time as a DateTime object. DateTime->now() returns 2024-08-28T12:34:56
DateTime->from_epoch() Creates a DateTime object from a Unix timestamp. DateTime->from_epoch(epoch => 1693227296) returns 2024-08-28T12:34:56
DateTime->ymd() Formats a DateTime object as 'YYYY-MM-DD'. $dt->ymd() returns 2024-08-28
DateTime->strftime() Formats a DateTime object using a strftime-style format string. $dt->strftime("%Y-%m-%d %H:%M:%S") returns 2024-08-28 12:34:56
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder