I am writing a little script that displays the information of a given certificate taken from a specific website:
$url = "http://www.google.com";
$orignal_parse = parse_url($url, PHP_URL_HOST);
$get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
$read = stream_socket_client("ssl://".$orignal_parse.":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
$cert = stream_context_get_params($read);
$certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
echo "Name: ".$certinfo['name']."<br/>";
echo "Subject: ".$certinfo['subject']['C']." ".$certinfo['subject']['ST']." ".$certinfo['subject']['CN']."<br/>";
echo "Issuer: ".$certinfo['issuer']['C']." ".$certinfo['subject']['O']." ".$certinfo['subject']['CN']."<br/>";
echo "Version: ".$certinfo['version']."<br/>";
echo "Valid from: ".$certinfo['validFrom']." to ".$certinfo['validTo']."<br/>";
For a full description of what the arrays returns go here: How to get SSL certificate info with CURL in PHP?
The problem is that $certinfo['validFrom']
returns a value like this:
170904000000Z
And I ahave no idea what it is. What I want is to convert $certinfo['validFrom']
and $certinfo['validTo']
to a human date format, but I don't know what type of time this is, I thought it was Unix but it didn't work, then I noticed that there is a "Z" at the end of the string:
echo "Valid from: ".gmdate("Y-m-d",$certinfo['validFrom'])." to ".gmdate("Y-m-d",$certinfo['validTo'])."<br/>";
returns: Valid from: 7385-09-24 to 7683-11-24
And that is obviously wrong.
I checked the certificate on the browser and the values:
Valid from: 170904000000Z to 180313235959Z
Well converted should be
Valid from 2017-09-03 to 2017-03-13
Thanks