# perldoc > HTTP::Date

---
type: CommandReference
command: HTTP::Date
mode: perldoc
section: ''
source: perldoc
---

## Quick Reference

- `$string = time2str($time)` — convert epoch time to HTTP date string (GMT)
- `$time = str2time($string)` — parse HTTP date string to epoch time
- `$time = str2time($string, $zone)` — parse with default timezone
- `@vals = parse_date($string)` — return list ($year, $month, $day, $hour, $min, $sec, $tz)
- `$string = time2iso($time)` — convert to ISO 8601 in local timezone
- `$string = time2isoz($time)` — convert to ISO 8601 in UTC

## Name

`HTTP::Date` — date conversion routines (version 6.05)

## Synopsis

perl
use HTTP::Date;

$string = time2str($time);    # Format as GMT ASCII time
$time   = str2time($string);  # convert ASCII date to machine time
## Functions

### `time2str($time)`

Converts epoch seconds to a string in HTTP format (RFC 1123, GMT). If called without arguments or with `undef`, uses current time. Example output: `Sun, 06 Nov 1994 08:49:37 GMT`.

### `str2time($string, $zone)`

Converts a date string to epoch time. Returns `undef` if unrecognized. Recognizes all formats listed under `parse_date()`. The optional `$zone` argument provides a default timezone (ignored if zone appears in string). If no zone specified and none in string, local timezone is assumed. Non-numerical zones (like "EST") require the `Time::Zone` module.

### `parse_date($string)`

Parses a date string and returns a list: `($year, $month, $day, $hour, $min, $sec, $tz)`. `$year` is 4-digit, `$month` starts at 1 (January). In scalar context, returns a string `"YYYY-MM-DD hh:mm:ss TZ"`. Returns empty list / `undef` if unrecognized. Recognized formats include:

- `"Wed, 09 Feb 1994 22:23:32 GMT"` — HTTP format
- `"Thu Feb 3 17:03:55 GMT 1994"` — ctime(3) format
- `"Thu Feb 3 00:00:00 1994"` — ANSI C asctime()
- `"Tuesday, 08-Feb-94 14:15:29 GMT"` — old RFC850
- `"Tuesday, 08-Feb-1994 14:15:29 GMT"` — broken RFC850
- `"03/Feb/1994:17:03:55 -0700"` — common logfile
- `"09 Feb 1994 22:23:32 GMT"` — HTTP (no weekday)
- `"08-Feb-94 14:15:29 GMT"` — RFC850 (no weekday)
- `"1994-02-03 14:15:29 -0100"` — ISO 8601
- `"1994-02-03"` — date only
- `"19940203T141529Z"` — ISO 8601 compact
- `"Feb 3 1994"` — Unix `ls -l` format
- `"11-15-96 03:52PM"` — Windows `dir` format

Leading/trailing whitespace is ignored. Seconds may be missing, month may be numeric. If year is missing, assumes the first matching date before current month. Two-digit years are resolved to the century closest to the current date.

### `time2iso($time)`

Like `time2str()`, but returns `"YYYY-MM-DD hh:mm:ss"` in the local timezone.

### `time2isoz($time)`

Like `time2str()`, but returns `"YYYY-MM-DD hh:mm:ssZ"` in UTC.

## Examples

perl
use HTTP::Date;

# Convert current time to HTTP string
print time2str();   # e.g. "Sun, 06 Nov 1994 08:49:37 GMT"

# Parse an HTTP date
$epoch = str2time("Sun, 06 Nov 1994 08:49:37 GMT");
print $epoch;       # 784111777

# Parse with default timezone
$epoch = str2time("06 Nov 1994 08:49:37", "GMT");

# Parse returns list
my @parts = parse_date("1994-02-03 14:15:29 -0100");
# @parts = (1994, 2, 3, 14, 15, 29, "-0100")
## See Also

- `time` in perlfunc
- [Time::Local](http://localhost/phpMan.php/perldoc/Time%3A%3ALocal/markdown)
- [Time::Zone](http://localhost/phpMan.php/perldoc/Time%3A%3AZone/markdown)

## Author

Gisle Aas <gisle@activestate.com>

## Copyright and License

Copyright (c) 1995-2019 by Gisle Aas. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.