zoukankan      html  css  js  c++  java
  • PHP--时间格式处理

    Ymd格式转Y-m-d或转成时间戳
    将Ymd格式如19930811转成1993-08-11格式
    date('Y-m-d',strtotime('19930811')
    将Ymd格式如19930811转成时间戳格式(注意:直接转会少一个小时即60*60秒),所以要加上3600
    strtotime('19930811')+3600




    /**
     * 获取统计时间
     * @param $type
     * 1 上月
     * 2 本月
     * 3 近15天
     * 4 近30天
     * @return array
     */
    function getDateInfo($type)
    {
      $data = array(
        array(
          'firstday' => date('Ym01', strtotime('-1 month')),
          'lastday' => date('Ymt', strtotime('-1 month')),
        ),
        array(
          'firstday' => date('Ym01', strtotime(date("Y-m-d"))),
          'lastday' => date('Ymd', strtotime((date('Ym01', strtotime(date("Y-m-d")))) . " +1 month -1 day")),
        ),
        array(
          'firstday' => date('Ymd', strtotime("-15 day")),
          'lastday' => date('Ymd', strtotime('-1 day')),
        ),
        array(
          'firstday' => date('Ymd', strtotime("-30 day")),
          'lastday' => date('Ymd', strtotime('-1 day')),
        ),
      );
      return is_null($type) ? $data : $data[$type-1];
    }
    
    
    


    //
    设置中国时区 date_default_timezone_set('PRC'); //今天的时间搓 $today_start = strtotime(date('Y-m-d',time()).' 0:0:0'); $today_end = strtotime(date('Y-m-d',time()).' 23:59:59'); //昨天的时间戳 $yesterday_start = strtotime('-1 day'.' 0:0:0'); $yesterday_end = strtotime('-1 day'.' 23:59:59'); //查看上个月日期 $first_day_of_month = date('Y-m',time()).'-01 00:00:01'; $t = strtotime($first_day_of_month); date('Y-m',$t); date('Y-m',strtotime('- 1 month',$t)); date('Y-m',strtotime('- 2 month',$t)); //获取时间戳 方法一: $yesterday_s = mktime(0,0,0,date('m'),date('d')-1,date('Y'));//昨天开始的时间搓 $yesterday_e = mktime(23,59,59,date('m'),date('d')-1,date('Y'));//昨天结束的时间搓 $tenday_s = mktime(0,0,0,date('m'),date('d')-10,date('Y'));//10天前开始的时间戳 $tenday_e = mktime(23,59,59,date('m'),date('d')-10,date('Y'));//10天前结束的时间戳 //方法二: $yesterday_s = strtotime(date("Y-m-d",strtotime("-10 day")).' 0:0:0'); //10天前开始的时间戳 $yesterday_e = strtotime(date("Y-m-d",strtotime("-10 day")).' 23:59:59');//10天前结束的时间戳 //方法三: $day = $_REQUEST['day'];//需要统计的日期 $time_s = strtotime($day.' 0:0:0'); $time_e = strtotime($day.' 23:59:59'); //获取本周一的时间戳 strtotime(date("Y-m-d",strtotime("-1 week Monday"))); 1. //获取今日开始时间戳和结束时间戳 2. $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y')); 3. $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1; 4. 5. //获取昨日起始时间戳和结束时间戳 6. $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y')); 7. $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1; 8. 9. //获取本周起始时间戳和结束时间戳 10. $beginThisweek = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y')); 11. $endThisweek=time(); 12. 13. //获取上周起始时间戳和结束时间戳 14. $beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')); 15. $endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')); 16. 17. //获取本月起始时间戳和结束时间戳 18. $beginThismonth=mktime(0,0,0,date('m'),1,date('Y')); 19. $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y')); 20. 21. //上个月的起始时间: 22. $begin_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month'))); 23. $end_time = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day'))); 24. 25. $begin_year = strtotime(date("Y",time())."-1"."-1"); //本年开始 26. $end_year = strtotime(date("Y",time())."-12"."-31"); //本年结束 27. 28. //现在的时间到第二天凌晨相差的时间戳 29. $time = (strtotime(date('Y-m-d'))+3600*24) - time() ;


  • 相关阅读:
    1058 A+B in Hogwarts (20分)
    我的Vue之小功能统计
    H5如何用Canvas画布生成并保存带图片文字的新年快乐的海报
    微信小程序之特殊效果及功能
    移动端H5适配方法(盒子+图片+文字)
    5分钟教你3种实现验证码功能
    微信小程序动态生成保存二维码
    微信授权获取code(微信支付)
    H5微信自定义分享链接(设置标题+简介+图片)
    带你走近WebSocket协议
  • 原文地址:https://www.cnblogs.com/mrszhou/p/8594616.html
Copyright © 2011-2022 走看看