Parsing HTTP 'Last-Modified' Date string in PHP
Asked Answered
F

3

5

I am using FileAPI to get the HTTP-header Last-Modified time of a file, which is returning the following string:

Fri Oct 25 2013 12:04:10 GMT+0100 (GMT Daylight Time)

This is then posted to PHP and I need it converted to something sensible, preferably a timestamp. Before you suggest it, strtotime() returns FALSE.

Can't seem to find any answer to this anywhere.

Flutterboard answered 6/12, 2013 at 16:30 Comment(4)
echo strtotime('Fri Oct 25 2013 12:04:10 GMT+0100'); => 1382699050. strtotime() IS able to parse this date format...Mccown
The string including the (GMT Daylight Time) as above? That's the complete string.Flutterboard
@MarcellFülöp 3v4l.org/CStMBMacaluso
Thank you @Macaluso - that explains the problem I'm seeing - php version here is 5.3.3. Please add this as an answer so I can accept - I think it's the most useful explanation to anyone else encountering this problem.Flutterboard
M
8

Fortunately since 5.3.0 there is DateTime::createFromFormat(). Although it cannot parse the trailing information it is at least able to ignore it using the + specifier. You don't need that information in order to create a timestamp as you already have the, machine parsable, GMT+0100.

Example:

$str = "Fri Oct 25 2013 12:04:10 GMT+0100 (GMT Daylight Time)";
$fmt = 'D M d Y H:i:s O+';

$datetime = DateTime::createFromFormat($fmt, $str);
echo $datetime->getTimestamp();

Output:

1382699050
Macaluso answered 6/12, 2013 at 16:43 Comment(5)
Thanks, although the regex wouldn't match until I removed the initial .*Flutterboard
Once you have $str from the preg_replace call, can't you just use strtotime($str) to get the timestamp? I'm doing that, and then date('Y-m-d H:i:s', $timestamp) to format the date for a mysql query, without problem (so far :) ). Also, can this question be reworded to replace 'Formatting' to 'Parsing'? Formatting sounds like converting data to a string, but you are actually trying to parse a string header to a value.Topless
@Topless The preg_replace() isn't necessary. Check my update. I should have read the documentation more carefully when I've answered this. ;) You are right with the title of the question. I've changed this.Macaluso
"strtotime() is able to parse the string only until PHP version 5.2.17" - this is untrue. I am using PHP 5.6.20 and I am able to use it fine: strtotime("Tue, 15 Nov 1994 12:45:26 GMT");.Wapiti
I see. It's an old answer, I don't know where I had this fromMacaluso
C
1

Well, if the problem is the string inside the parentheses, you could substring it to the first one and use strtotime as @Marcell suggested in the comments.

Confidante answered 6/12, 2013 at 16:37 Comment(0)
E
0

This is a late answer but the accepted answer is based on the OP's question and not on the real format used by Last-Modified header. There is a missing comma (,) after the day of the week and the data order is not right.

The official format for Last-Modified header is:

Last-Modified: <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT

Therefore, the date format you get from the header should be different and the format to use already exists in the DateTime interface as DateTimeInterface::RFC7231:

$str = "Fri,25 Oct 2013 12:04:10 GMT";
$datetime = DateTime::createFromFormat($fmt, $str);

echo $datetime->getTimestamp();

I know this is different from what the OP asked but the HTTP protocol has a different format than what was written by the OP.

This answer should be seen as complementary.

Epagoge answered 9/1 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.