---
title: "通过Internet给系统校时的小脚本（PHP）"
author: "wj45"
date: "2010-10-27T15:33:55Z"
tags: []
categories: ["编程语言"]
slug: "internet-php"
draft: false
lastmod: "2010-10-27T15:34:21Z"
id: 795593
url: "https://wj45.iteye.com/blog/795593"
---

灰常简单的一个小脚本，Linux/Windows系统都支持，调用了一下百度的开放平台的一个标准北京时间的小接口。

没事跑跑这个小脚本给系统校时还是很方便的。


```php

<?php
//系统校时
$hd = fopen("http://open.baidu.com/app?module=beijingtime", 'r');
$str = fgets($hd);
$pos = strpos($str, '"time":');
$str = str_replace('"time":', '', $str);
$str = substr($str, $pos, 10);
$time = floatval($str);
date_default_timezone_set('Asia/Shanghai');
$ntime = date('H:i:s', $time);
if (PHP_OS == 'Linux') {
	$ndate = date('Y/m/d', $time);
	echo system("date -s '$ndate $ntime'");
} else {
	$ndate = date('Y-m-d', $time);
	echo system("date $ndate");
	echo system("time $ntime");
}
echo $ndate . ' ' . $ntime;
?>

```