前言
高考完在家属实不知道干啥了(出去玩??尼玛没考好咋找我爹要钱)说白了就是闲的不行
思路
大致思路就是先获取日出和日落的时间,然后和当时时间作比较,emmmmm我找了半天(三分钟)也没有找到这样的输出日出日落时间的API(额我好像找到了可以做的事情了,我找了世界时间网,这里是北京时间可能会与人们当地时间不一样,正则获取
代码
<?php
function get_curl($url, $post = 0, $referer = 0, $cookie = 0, $header = 0, $ua = 0, $nobody = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$httpheader[] = "Accept:*/*";
$httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
$httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
$httpheader[] = "Connection:close";
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if ($header) {
curl_setopt($ch, CURLOPT_HEADER, 1);
}
if ($cookie) {
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if ($referer) {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
if ($ua) {
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0");
}
if ($nobody) {
curl_setopt($ch, CURLOPT_NOBODY, 0);
}
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
//获取源代码
$res = get_curl('https://mtime.guowaitianqi.com/beijing');
//获取日出时间
preg_match("/\日出时间:(.*)日落时间/",$res,$sunup);
$sunup = date('Y-m-d').' '.$sunup[1];
//获取日落时间
preg_match("/\日落时间:(.*)</",$res,$sundown);
$sundown = date('Y-m-d').' '.$sundown[1];
echo $sunup;
echo $sundown;
然后就是判断与当前时间的大小咯
//判断是否开启夜间模式
$time = time();
if($time<strtotime($sunup) || $time>strtotime($sundown)){
//开启夜间模式
}else{
//关闭夜间模式
}