取出本周的周一和周日的时间戳

 更新时间:2016年11月25日 15:56  点击:2120

方法一
<?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;
    }
}

其实于php file_exists 函数与 file_exists语法我们早就讲过了,下面我们来看看一下关于它的使用方法与实例吧

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);
}

[!--infotagslink--]

相关文章

  • 在java中获取List集合中最大的日期时间操作

    这篇文章主要介绍了在java中获取List集合中最大的日期时间操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
  • 教你怎么用Java获取国家法定节假日

    这篇文章主要介绍了教你怎么用Java获取国家法定节假日,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下...2021-04-23
  • PostgreSQL TIMESTAMP类型 时间戳操作

    这篇文章主要介绍了PostgreSQL TIMESTAMP类型 时间戳操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-26
  • mysql中获取一天、一周、一月时间数据的各种sql语句写法

    创建表:复制代码 代码如下:create table if not exists t( id int, addTime datetime default '0000-00-00 00:00:00′)添加两条初始数据:insert t values(1, '2012-07-12 21:00:00′);insert t values(2, '2012-07...2014-05-31
  • .NET/C# 使用Stopwatch测量运行时间

    这篇文章主要介绍了.NET/C# 使用Stopwatch测量运行时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • 解决python 两个时间戳相减出现结果错误的问题

    这篇文章主要介绍了解决python 两个时间戳相减出现结果错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-12
  • 常用的日期时间正则表达式

    常用的日期时间正则表达式 下面收藏了大量的日期时间正则匹配函数,包括分钟,时间与秒都能达到。 正则表达式 (?n:^(?=d)((?<day>31(?!(.0?[2469]|11))|30(?!.0?2)|29(...2016-11-25
  • 非常全面的php日期时间运算汇总

    实例讲解之前,先来介绍几个核心函数: mktime 函数 mktime() 函数返回一个日期的 Unix 时间戳。 参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。 参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。...2015-11-08
  • C#中动态显示当前系统时间的实例方法

    想在网页中动态地显示当前系统的时间,找了好多,不过都是一些停在那里不动的。。。不过皇天不负有心人,终于让我找到了...2020-06-25
  • postgresql 中的时间处理小技巧(推荐)

    这篇文章主要介绍了postgresql 中的时间处理小技巧(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • 从chrome调试工具中把拖延时间的东西找出来

    我打开android开发手册的时候:http://www.csdn123.com/html/android/reference/packages.html 发现打开速度很慢,我用按了一下F12打开调试面板,切换到网络的选项卡network...2016-05-19
  • C# 当前系统时间获取及时间格式详解

    这篇文章主要介绍了C# 当前系统时间获取及时间格式详解的相关资料,这里提供代码实例,帮助大家学习参考,需要的朋友可以参考下...2020-06-25
  • 帝国CMS显示指定时间内更新的信息数量

    /*解决代码高亮太长不换行*/ .syntaxhighlighter{word-break:break-all;} uParse('#newstext', {rootPath: '/e/extend/ueditor/'}) 帝国CMS显示指定时间内更新的信息数...2016-11-01
  • C#使用TimeSpan时间计算的简单实现

    这篇文章主要给大家介绍了关于C#使用TimeSpan时间计算的相关资料,以及通过一个实例代码给大家介绍了C#使用timespan和timer完成一个简单的倒计时器的方法,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧...2020-06-25
  • php根据日期或时间戳获取星座信息和生肖等信息

    分享一个利用php根据日期或时间戳获取相应的干支纪年,生肖和星座信息的函数方法,具体函数代码以及使用方法如下: /** 判断干支、生肖和星座 */ function birthext($birth){ if(strstr($birth,'-')===false&&strlen($bi...2015-10-21
  • C#获取文件创建时间的方法

    这篇文章主要介绍了C#获取文件创建时间的方法,涉及C#文件操作的技巧及CreattionTime属性的使用方法,需要的朋友可以参考下...2020-06-25
  • PowerShell中使用Get-Date获取日期时间并格式化输出的例子

    这篇文章主要介绍了PowerShell中使用Get-Date获取日期时间并格式化输出的例子,本文讲解了直接调用Get-Date、在Write-Host中使用Get-Date、格式化输出的方法,需要的朋友可以参考下...2020-06-30
  • php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法

    php获取今日开始时间戳和结束时间戳$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;//php获取昨日起始时间戳和结束时间...2013-10-04
  • Unity时间戳的使用方法

    这篇文章主要为大家详细介绍了Unity时间戳的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • JavaScript时间操作之年月日星期级联操作

    这篇文章主要介绍了JavaScript时间操作之级联日期选择操作,涉及到年、月、日、星期,感兴趣的小伙伴们可以参考一下...2016-01-18