For more information... RTFM!
NAVIGATION
PAGES THAT LINK HERE
ACCOUNT LOGIN

You are not logged in

Powered by Interchange version 5.7.0

datetime2epoch

Convert a date (possibly with a time) to the number of seconds since the Epoch (1970-01-01 00:00:00 UTC).

The date/time is expected to have one of the following formats:

MM[/-]DD[/-]YY(YY)?(:hh(:?mm(:ss)?)?)?

or (ISO/MySQL date/time):

YYYY-MM-DD([T ]hh(:mm(:ss)?)?)?

First format:  if the year specification contains only two digits, and is less than 50, then it is treated as an offset from the year 2000, rather than from 1900.  In other words, 07 is understood as year 2007, and 80 is understood as year 1980.  An unspecified day or month will default to 01.  Unspecified hours or minutes default to 00.

Second (ISO/MySQL date/time) format:  The year must be specified using four digits, and the day and month are not optional.

In both of the above cases, and unless specified otherwise, the hours, minutes and seconds will default to 00.

To convert the seconds-since-epoch value back into a date, see the "strftime" filter.

Availability

Availability

This filter was introduced in version 5.5.0, and is therefore not available for use with any earlier Interchange version.

This filter was designed to replace the "date2time" filter.

Example

  1. [filter datetime2epoch]01/01/2005[/filter]
  2. [filter datetime2epoch]01/01/05[/filter]
  3. [filter datetime2epoch]01-01-2005[/filter]
  4. [filter datetime2epoch]01-01-05[/filter]
  5. [filter datetime2epoch]01-01-2005:10[/filter]
  6. [filter datetime2epoch]01-01-05:1000[/filter]
  7. [filter datetime2epoch]01-01-05:1045[/filter]
  8. [filter datetime2epoch]01-01-05:10:45[/filter]
  9. [filter datetime2epoch]01-01-05:10:45:15[/filter]
  10. [filter datetime2epoch]2005-01-01[/filter]
  11. [filter datetime2epoch]2005-01-01 10:45[/filter]
  12. [filter datetime2epoch]2005-01-01 10:45:15[/filter]
  13. [filter datetime2epoch]2005-01-01T10:45:15[/filter]

Results in:

  1. 1104537600
  2. 1104537600
  3. 1104537600
  4. 1104537600
  5. 1104573600
  6. 1104573600
  7. 1104576300
  8. 1104576300
  9. 1104576315
  10. 1104537600
  11. 1104576300
  12. 1104576315
  13. 1104576315

Source code

sub {
    use Time::Local;

    my ($year, $mon, $day, $hr, $min, $sec, $time);

    my $val = shift;
    $val =~ s/\0+//g;

    $val =~ m%^\s*(\d\d)[-/]+(\d+)[-/]+(\d+)% and do {
        ($year, $mon, $day) = ($3, $1, $2);

        $val =~ /:(\d\d):?(\d\d)?:?(\d\d)?\s*$/
            and $time = sprintf('T%02d:%02d:%02d', $1, $2 || 0, $3 || 0);

        if (length($year) < 4) {
            $year =~ s/^0//;
            $year = $year < 50 ? $year + 2000 : $year + 1900;
        }
        $val = sprintf('%d-%02d-%02d%s', $year, $mon || 1, $day || 1, $time);
    };

    $val =~ /^\s*(\d\d\d\d)-(\d\d)-(\d\d)(?:[T\s](\d\d)?(?::(\d\d)?(?::(\d\d)?)?)?)?/;
    ($year, $mon, $day, $hr, $min, $sec) = ($1, $2, $3, $4 || 0, $5 || 0,$6 || 0);
    eval {
        $time = timelocal($sec, $min, $hr, $day, --$mon, $year);
    };
    if ($@) {
        logError("bad time value passed to datetime2epoch: %s", $@);
        return 0;
    }
    return $time;
}

Category:  Filters
Last modified by: Kevin Walsh
Modification date: Friday 23 February 2007 at 3:04 PM (EST)
Home  |  Legal nonsense  |  Privacy policy  |  Contact us