문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top