试图从日期对象获取年/月/日,但它返回1970-01-01

时间:2018-06-12 12:36:49

标签: php

我正在尝试使用返回年,月和日的脚本,但响应始终相同:1970-01-01

我无法理解,它应该在每个链接上更改并且“google”ssl到期日期不是1970-01-01

那么,有人可以在代码中看到错误吗?

$url           = "https://www.google.es/";
$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']);
$valid_to = date(DATE_RFC2822, $certinfo['validTo_time_t']);
$time     = strtotime($valid_to);
$month    = date("F",$time);
$year     = date('Y', $time);
$day      = date('j', $time);

if ($month = "Januay") {
    $month = "01";
} else if ($month = "February") {
    $month = "02";
} else if ($month = "March") {
    $month = "03";
} else if ($month = "April") {
    $month = "04";
} else if ($month = "May") {
    $month = "05";
} else if ($month = "June") {
    $month = "06";
} else if ($month = "July") {
    $month = "07";
} else if ($month = "August") {
    $month = "08";
} else if ($month = "September") {
    $month = "09";
} else if ($month = "October") {
    $month = "10";
} else if ($month = "November") {
    $month = "11";
} else if ($month = "December") {
    $month = "12";
}
$strtime = strtotime($day ."/" . $month . "/" . $year);
$newdate = date('Y-m-d',$strtime);
echo $newdate;

2 个答案:

答案 0 :(得分:1)

strtotime函数使用日期各部分之间的字符来确定它是美国格式还是欧洲格式。你的电话是使用斜线,所以strtotime正在寻找月/日/年,但你正在通过日/月/年。尝试更改

$strtime = strtotime($day ."/" . $month . "/" . $year);

$strtime = strtotime($month ."/" . $day . "/" . $year);

答案 1 :(得分:1)

在我看来,你的代码已经过度工作了 日期和strtotime意味着一起工作,但由于某种原因,你似乎将它们分开。
保持这个:

$url           = "https://www.google.es/";
$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']);
$valid_to      = date(DATE_RFC2822, 
$certinfo['validTo_time_t']);
$time = strtotime($valid_to);

然后你需要:

$newdate = date('Y-m-d',$time);

你完成了!