方法一
<?php教程
echo strtotime('last Monday');
echo '<br />';
echo strtotime('next Sunday');
?>
方法二
<?
02 function getMonSun(){
03 $curTime=time();
04
05 $curWeekday = date('w');
06
07 //为0是 就是 星期七
08 $curWeekday = $curWeekday?$curWeekday:7;
09
10
11 $curMon = $curTime - ($curWeekday-1)*86400;
12 $curSun = $curTime + (7 - $curWeekday)*86400;
13
14 $cur['Mon'] = $curMon;
15 $cur['Sun'] = $curSun;
16
17 return $cur;
18 }
19 $cur = getMonSun();
20
21 echo date('Y-m-d',$cur['Mon']);
22 echo "rn";
23 echo date('Y-m-d',$cur['Sun']);
24 ?>
方法三
<?
02 function getMonSun(){
03 $curTime=time();
04
05 //求出当前是星期几:
06 $curWeekday = date('w');
07
08 //如果是周一则减上7*86400周二减上6*86400,依此类推得到周一的时间戳:
09 switch ($curWeekday) {
10 case 0:
11 $curMon = $curTime-7*86400;
12 $curSun = $curTime;
13 break;
14 case 1:
15 $curMon = $curTime;
16 $curSun = $curTime+6*86400;
17 break;
18 case 2:
19 $curMon = $curTime-1*86400;
20 $curSun = $curTime+5*86400;
21 break;
22 case 3:
23 $curMon = $curTime-2*86400;
24 $curSun = $curTime+4*86400;
25 break;
26 case 4:
27 $curMon = $curTime-3*86400;
28 $curSun = $curTime+3*86400;
29 break;
30 case 5:
31 $curMon = $curTime-4*86400;
32 $curSun = $curTime+2*86400;
33 break;
34 case 6:
35 $curMon = $curTime-5*86400;
36 $curSun = $curTime+1*86400;
37 break;
38 }
39 $cur['Mon'] = $curMon;
40 $cur['Sun'] = $curSun;
41
42 return $cur;
43 }
44 $cur = getMonSun();
45 echo date('Y-m-d',$cur['Mon']);
46 echo "rn";
47 echo date('Y-m-d',$cur['Sun']);
48 ?>
<?php教程
$arr=array(
0=>array('title' => '新闻1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'),
1=>array('title' => '新闻2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM')
);
?>
如果你想统计数组$arr的长度,也就是说该二维数组只有两条新闻,你想要的数字也是2,但是如果使用count($arr)不同版本的php,统计的结果是不一样的;
后来在php手册中发现,count函数还有第二个参数,解释如下:
count函数有两个参数:
0(或COUNT_NORMAL)为默认,不检测多维数组(数组中的数组);
1(或COUNT_RECURSIVE)为检测多维数组,
所以如果要判断读取的数组$arr是不是有新闻信息,就要这样写了:
<?php
if(is_array($arr) && count($arr,COUNT_NORMAL)>0 )
{
.....
} else {
.....
}
?>
你可以使用这样的代码来测试该函数:
<?php
$arr=array(
0=>array('title' => '新闻1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'),
1=>array('title' => '新闻2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM')
);
echo '不统计多维数组:'.count($arr,0);//count($arr,COUNT_NORMAL)
echo "<br/>";
echo '统计多维数组:'.count($arr,1);//count($arr,COUNT_RECURSIVE)
?>
好了,到此为止,已经解决php中获取二维或多维数组的第一维长度的问题!
try{
$tor = new TorHttp('127.0.0.1', 9050);
$data = array('username' => 'liujun');
$headers = array('Cookie' => 'php教程id=123456; xx=v');
echo $tor->get('http://host.com/testsocks.php', $headers);
$tor->newId('123456');
}catch(Exception $e){
if($e->getCode() == 9999){
$tor->newId('123456');
}
echo $e->getMessage() . "n";
}
*/
class Tool_TorHttp
{
/**
* Tor提供的socks服务器
*
* @var <string
*/
private $_host;
/**
* socks服务的连接
*
* @var <stream>
*/
private $_sock;
/**
* 构造函数
*
* @param <string> $host socks服务器地址
* @param <int> $port
*/
public function __construct($host = '127.0.0.1', $port = 9050)
{
$this->_host = $host;
@ $this->_sock = fsockopen($host, $port, $errorCode, $error, 5);
if($errorCode){
throw new Exception('不能连接代理服务器');
}
//建立应用层的连接
fwrite($this->_sock, pack('C3', 5, 1, 0));
$resp = fread($this->_sock, 1024);
$resp = unpack('Cversion/Cmethod', $resp);
if($resp['version'] != 5 || $resp['method'] != 0){
$this->_sock = null;
throw new Exception('代理服务器不可用或者需要连接密码');
}
}
/**
* 连接目标主机
*
* @param <type> $host
* @param <type> $port
*/
private function _connect($host, $port)
{
//ip和域名描述的服务器用不同的报文格式
$lip = ip2long($host);
if(empty($lip)){
$pack = pack('C5', 5, 1, 0, 3, strlen($host)) . $host . pack('n', intval($port));
}else{
$pack = pack('C4Nn', 5, 1, 0, 1, $lip, $port);
}
fwrite($this->_sock, $pack);
$resp = '';
$counter = 0;
while(true){
if(feof($this->_sock)){
break;
}
$resp .= fread($this->_sock, 1024);
if(!empty($resp) || $counter == 50){
break;
}
$counter += 1;
}
$resp = unpack('Cversion/Crep', $resp);
if(($resp['version'] != 5) || ($resp['rep'] != 0)){
throw new Exception("请求的服务[$host:$port]暂时不可用,或者Tor不能到达,如果反复发生,请尝试重启Tor", 9999);
}
}
/**
* 发起一次http的get请求
*
* @param <type> $url
* @return <string>
*/
public function get($url, $headers = array())
{
$ua = parse_url($url);
if(empty($ua['port'])){
$ua['port'] = 80;
}
$this->_connect($ua['host'], $ua['port']);
$requestUri = $ua['path'];
if(!empty($ua['query'])){
$requestUri .= '?' . $ua['query'];
}
$headers['Host'] = $ua['host'];
if(!isset($headers['User-Agent'])){
$headers['User-Agent'] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 GTB5";
}
$headers['Connection'] = 'close';
fwrite($this->_sock, "GET {$requestUri} HTTP/1.1n");
foreach ($headers as $key => $val){
fwrite($this->_sock, "{$key}: {$val}n");
}
fwrite($this->_sock, "n");
$resp = '';
while(!feof($this->_sock)){
$resp .= fread($this->_sock, 1024);
}
return $resp;
}
/**
* 发起一次http的post请求
*
* @param <type> $url
* @param <type> $data
* @param <type> $headers
* @return <type>
*/
public function post($url, $data, $headers = array())
{
$ua = parse_url($url);
if(empty($ua['port'])){
$ua['port'] = 80;
}
$this->_connect($ua['host'], $ua['port']);
if(isset($ua['path']) && !empty($ua['path'])){
$requestUri = $ua['path'];
}else{
$requestUri = '/';
}
if(!empty($ua['query'])){
$requestUri .= '?' . $ua['query'];
}
if(!isset($headers['User-Agent'])){
$headers['User-Agent'] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 GTB5";
}
$headers['Connection'] = 'close';
$data = $this->_parseData($data);
fwrite($this->_sock, "POST {$requestUri} HTTP/1.1n");
fwrite($this->_sock, "Host: {$ua['host']}n");
fwrite($this->_sock, "Content-Type: application/x-www-form-urlencodedn");
fwrite($this->_sock, "Content-Length: " . strlen($data) . "n");
foreach ($headers as $key => $val){
fwrite($this->_sock, "{$key}: {$val}n");
}
fwrite($this->_sock, "n");
fwrite($this->_sock, $data . "n");
$resp = '';
while(!feof($this->_sock)){
$resp .= fread($this->_sock, 1024);
}
return $resp;
}
/**
* 更新Tor的身份
*
* @param <type> $password
* @param <type> $port
* @return <type>
*/
public function newId($password = '', $port = 9051)
{
if(!empty($password) && $password[0] != '"'){
$password = '"' . $password . '"';
}
//创建到tor控终端的连接
@ $sock = fsockopen($this->_host, $port, $errorCode, $error, 5);
if($errorCode){
throw new Exception('不能连接代理服务器控制端,请检查端口号');
}
fwrite($sock, "AUTHENTICATE {$password}n");
$resp = fread($sock, 1024);
if(!preg_match('/^250/', $resp)){
throw new Exception('Tor控制认证失败,请确认密码正确');
}
fwrite($sock, "SIGNAL NEWNYMn");
$resp = fread($sock, 1024);
if(!preg_match('/^250/', $resp)){
throw new Exception('更新身份失败,请重试');
}
return true;
}
private function _parseData($data)
{
if(empty($data) || !is_array($data)){
return $data;
}
$encoded = '';
while (list($k, $v) = each($data)) {
$encoded .= $k . "=" . $v . '&';
}
return substr($encoded, 0, -1);
}
public function __destruct()
{
fclose($this->_sock);
$this->_sock = null;
}
}
bool file_exists ( string filename )
如果由 filename 指定的文件或目录存在则返回 TRUE,否则返回 FALSE。
其实于php教程 file_exists 函数与 file_exists语法我们早就讲过了,下面我们来看看一下关于它的使用方法与实例吧
路径的文件或目录。
在Windows上,使用/ /计算机名/共享/文件名或 计算机名共享文件名,以检查网络共享文件。
这是一个很简单的实例一
<?php
$filename = '/www.111cn.net/aa/to/foo.txt';
if (file_exists($filename)) {
echo "文件$filename exists";
} else {
echo "文件$filename 不存在";
}
?>
输出结果为:
文件/www.111cn.net/aa/to/foo.txt己存在
再来看看实例二
<?php
echo file_exists("www.111cn.net.txt");
?>
这个我们就直接用file_exists来返回ture or false
PHP程序的缓冲,而不论PHP执行在何种情况下(CGI ,web服务器等等)。该函数将当前为止程序的所有输出发送到用户的浏览器。
flush() 函数不会对服务器或客户端浏览器的缓存模式产生影响。因此,必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
个别web服务器程序,特别是Win32下的web服务器程序,在发送结果到浏览器之前,仍然会缓存脚本的输出,直到程序结束为止
*/
for ($i=10; $i>0; $i--)
{
echo $i;
ob_flush();
flush();
sleep(1);
}
// Microsoft Internet Explorer 只有当接受到的256个字节以后才开始显示该页面,所以必须发送一些额外的空格来让这些浏览器显示页面内容
//正常用法
ob_start();
ob_end_flush();
for($i = 1; $i <= 300; $i++ )echo ' www.111cn.net';
$i = 0;
while(1) {
echo $i;
sleep(1);
$i++;
}
for($i=0;$i<10;$i++) {
echo $i;
flush();
sleep(1);
}
//这段代码,应该隔一秒钟输出一次$i,但是实际中却不一定是这样,IE浏览器下有可能是等了10秒钟后,所有的输出同时呈现出来
//ob_end_flush();//IE8下没起作用
echo str_pad(" ", 256);//IE需要接受到256个字节之后才开始显示
for($i=0;$i<18;$i++) {
echo $i;
flush();
sleep(1);
}