• mktime 和strtotime获取今日、昨日、上周、本月的起始时间戳和结束时间戳,


    首先介绍一下 mktime 函数作用和用法。
    mktime() 函数用于返回一个日期的 Unix 时间戳。
    mktime(hour,minute,second,month,day,year,is_dst)
    参数 描述
    hour 可选。规定小时。
    minute 可选。规定分钟。
    second 可选。规定秒。
    month 可选。规定用数字表示的月。
    day 可选。规定天。
    year 可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。
    is_dst 
    可选。如果时间在日光节约时间(DST)期间,则设置为1,否则设置为0,若未知,则设置为-1。自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。
    用法
    参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。
    参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。
    注意在 PHP 5.1 之前,如果该函数的参数非法,则会返回 false。
    另外需要注意的是该函数对于日期运算和验证非常有用。它可以自动校正越界的输入;

    strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。
    strtotime(time,now)
    参数 描述
    time 规定要解析的时间字符串。
    now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。
    用法
    该函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数,则用系统当前时间。
    该函数将使用 TZ 环境变量(如果有的话)来计算时间戳。自 PHP 5.1.0 起有更容易的方法来定义时区用于所有的日期/时间函数。此过程在 date_default_timezone_get() 函数页面中有说明。
    成功则返回时间戳,否则返回 FALSE。在 PHP 5.1.0 之前本函数在失败时返回 -1。
    提供此参数,则用系统当前时间。
    如:
    //php获取今日开始时间戳和结束时间戳
     
     $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
     echo '$beginToday:'.date('Y-m-d H:i:s',$beginToday).'<br>';
     $beginToday=strtotime('today');
     echo '$beginToday:'.date('Y-m-d H:i:s',$beginToday).'<br>';
      
     $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
     echo '$endToday:'.date('Y-m-d H:i:s',$endToday).'<br>';
     $endToday=strtotime('today 23:59:59');
     echo '$endToday:'.date('Y-m-d H:i:s',$endToday).'<br>';
      
     //php获取昨日起始时间戳和结束时间戳
      
     $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
     echo '$beginYesterday:'.date('Y-m-d H:i:s',$beginYesterday).'<br>';
     $beginYesterday=strtotime('yesterday');
     echo '$beginYesterday:'.date('Y-m-d H:i:s',$beginYesterday).'<br>';
      
     $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
     echo '$endYesterday:'.date('Y-m-d H:i:s',$endYesterday).'<br>';
     $endYesterday=strtotime('yesterday 23:59:59');
     echo '$endYesterday:'.date('Y-m-d H:i:s',$endYesterday).'<br>';
      
     //php获取上周起始时间戳和结束时间戳
      
     $beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
     echo '$beginLastweek:'.date('Y-m-d H:i:s',$beginLastweek).'<br>';
     $beginLastweek=strtotime('first day of last week 00:00:00');
     echo '$beginLastweek:'.date('Y-m-d H:i:s',$beginLastweek).'<br>';
     
     $endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
     echo '$endLastweek:'.date('Y-m-d H:i:s',$endLastweek).'<br>';
     $endLastweek=strtotime('this week -1 seconds 00:00:00');
     echo '$endLastweek:'.date('Y-m-d H:i:s',$endLastweek).'<br>';
      
     //php获取本月起始时间戳和结束时间戳
      
     $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
     echo '$beginThismonth:'.date('Y-m-d H:i:s',$beginThismonth).'<br>';
     $beginThismonth=strtotime('first day of this month 00:00:00');
     echo '$beginThismonth:'.date('Y-m-d H:i:s',$beginThismonth).'<br>';
      
     $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
     echo '$endThismonth:'.date('Y-m-d H:i:s',$endThismonth).'<br>';
     $endThismonth=strtotime('last day of this month 23:59:59');
     echo '$endThismonth:'.date('Y-m-d H:i:s',$endThismonth).'<br>';
  • 相关阅读:
    【跃迁之路】【461天】刻意练习系列220(2018.05.12)
    【跃迁之路】【461天】刻意练习系列220(2018.05.12)
    【跃迁之路】【461天】刻意练习系列220(2018.05.12)
    Ajax打开三种页面的请求
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/flowers-yang/p/3379721.html
Copyright © 2020-2023  润新知