曾在项目中需要使用到今天,昨天,本周,本月,本季度,今年,上周上月,上季度等等时间戳,趁最近时间比较充足,因此计划对php的相关时间知识点进行总结学习

1,阅读php手册date函数

  常用时间函数:

    checkdate()验证一个时间是否正确

    date_default_timezone_get()取得当前脚本所使用的时区

    date_default_timezone_set()设定脚本所用时区  ini_set()也可以满足,或者修改配置文件

    date_sunrise()  date_sunset() 返回给定的日期和地点的日出时间和日落时间

    date()格式化一个日期,下边会有详细内容

    getdate() 取得日期时间的相关信息

    gettimeofday()取得当前时间的相关信息

    idate()将本地时间日期格式化为整数,但只接受一个字符作为参数

    microtime()返回当前的时间戳和秒数

    mktime()取得一个日期的时间戳

    strtotime()将英文文本的日期秒数解析为时间戳

2,重要函数详解

  date()格式化一个日期

    string date( string $format [, int $timestamp] )

    d    月份中的第几天,也就是几号,此为具有前导零,例如01,02

    D    星期中的第几天,也就是英文星期几的单词缩写,Mon到Sun

    l(L小写) 星期几,此为完整的英文格式, Sunday到Saturday

    N    用数字表示星期几,1为星期一,7为星期日

    S    每月天数后面的英文后缀

    w    星期中的第几天,使用数字表示,0为星期天,6为星期六

    z    年份中的第几天 0到365

    W    年份中的第几周

    F    月份,完整的英文单词

    m    月份数字格式,具有前导0

    M    三个字母表示的缩写的月份

    n    数字表示的月份,没有前导0

    t    给定月份所应有的天数

    L    检测是否为闰年,闰年为1,月份为0

    Y    4位数字表示的年份

    y    2位数字表示的年份

    a    小写的上午或者下午的值

    A    大写的上午或者下午的值

    g    12小时制,没有前导0

    G    24小时制,没有前导0

    h    12小时制,有前导0

    H    24小时制,有前导0

    i    具有前导0的分钟数

    s    秒数,具有前导0

    u    毫秒,date()函数返回的是000000格式的

    e    时区标识

    I    是否为夏令时,是为1,不是为0

    T    本机所在的时区

    c    2017-05-08T 15:22:21+00:00 格式的时间

    U    从1970开始至今的秒数

  idate()函数详解

    与date的区别是此函数只可以传递一个参数,date()可以传递多个参数

    B    Internet time

    d    月份中的第几天

    h    12小时制的时间

    H    24小时制的时间

    i    分钟

    I    若启用夏令时返回1,否则为0

    L    如果是闰年则返回1,否则返回0

    m    月份的数字

    s    秒数

    t    本月的总天数

    U    从1970起的秒数

    w    星期中的第几天

    W    年份中的第几个星期,星期从星期一开始

    y    年份,1或者2位数字

    Y    年份4位数字

    z    年份中的第几天

    Z    以秒为单位的时区偏移量

  strtotime()函数衔接

    用法示例

      strtotime ("now");

      strtotime ("10 September 2017");

      strtotime ("+1 day");

      strtotime ("+1 week");

      strtotime ("+1 week 2 days 4 hours 2 seconds");

      strtotime ("next Thursday");

      strtotime ("last Monday");

3,常用时间汇总

    

$times = [];
function makeTime(){
//获取今日开始时间戳和结束时间戳
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
$times['today']['begin'] = $beginToday;
$times['today']['end'] = $endToday; //获取昨日起始时间戳和结束时间戳
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
$times['yesterday']['begin'] = $beginYesterday;
$times['yesterday']['end'] = $endYesterday; //获取上周起始时间戳和结束时间戳
$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
$times['lastWeek']['begin'] = $beginLastweek;
$times['lastWeek']['end'] = $endLastweek; //获取本月起始时间戳和结束时间戳
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
$times['thisMonth']['begin'] = $beginThismonth;
$times['thisMonth']['end'] = $endThismonth; //获取本周开始时间和结束时间,此例中开始时间为周一
$defaultDate = date('Y-m-d');
$first = 1;
$w = date('w',strtotime($defaultDate));
$beginWeek = strtotime("$defaultDate-" . ($w?$w-$first:6) . 'days');
$endWeek = $beginWeek + 6*24*3600-1;
$times['thisWeek']['begin'] = $beginWeek;
$times['thisWeek']['end'] = $endWeek; //获取上月的起始时间戳和结束时间戳
$beginLastmonth=mktime(0,0,0,date('m')-1,1,date('Y'));
$endLastmonth=mktime(23,59,59,date('m')-1,date('t'),date('Y'));
$times['LastMonth']['begin'] = $beginLastmonth;
$times['LastMonth']['end'] = $endLastmonth; //获取今年的起始时间和结束时间
$beginThisyear = mktime(0,0,0,1,1,date('Y'));
$endThisyear = mktime(23,59,59,12,31,date('Y'));
$times['thisYear']['begin'] = $beginThisyear;
$times['thisYear']['end'] = $endThisyear; //获取上年的起始时间和结束时间
$beginLastyear = mktime(0,0,0,1,1,date('Y')-1);
$endLastyear = mktime(23,59,59,12,31,date('Y')-1);
$times['lastYear']['begin'] = $beginLastyear;
$times['lastYear']['end'] = $endLastyear; //获取本季度开始时间和结束时间
$season = ceil((date('n'))/3);//当月是第几季度
$beginThisSeason = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));
$endThisSeason = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
$times['thisSeason']['begin'] = $beginThisSeason;
$times['thisSeason']['end'] = $endThisSeason; //获取上季度的起始时间和结束时间
$beginLastSeason = mktime(0, 0, 0,($season-1)*3-3+1,1,date('Y'));
$endLastSeason = mktime(23,59,59,($season-1)*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
$times['lastSeason']['begin'] = $beginLastSeason;
$times['lastSeason']['end'] = $endLastSeason; return $times;
}
$times = makeTime();

目前是我之前用到的时间戳,后期还会积累汇总,避免重复造轮子。希望大家一起学习

php 中时间函数date及常用的时间计算的更多相关文章

  1. PHP函数之日期时间函数date()使用详解

    date()函数是我们在php开发中常碰到并且会使用到的一个日期函数,下面我来给大家介绍date()函数的一些基本扮靓和方法,有需要了解的朋友可进入参考   日期时间函数是PHP 的核心组成部分.无需 ...

  2. (转)在网页中JS函数自动执行常用三种方法

    原文:http://blog.sina.com.cn/s/blog_6f6b4c3c0100nxx8.html 在网页中JS函数自动执行常用三种方法 在网页中JS函数自动执行常用三种方法 在HTML中 ...

  3. 在网页中JS函数自动执行常用三种方法

    在网页中JS函数自动执行常用三种方法 在HTML中的Head区域中,有如下函数: <SCRIPT   LANGUAGE="JavaScript">   function ...

  4. PHP中日期时间函数date()用法总结

    date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考. 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母 ...

  5. 【推荐】PHP中格式化时间函数date与gmdate的区别 | 修改PHP的默认时区

    PHP中的时间有2个格式化函数:date()和gmdate(),在官方的文档中的描述为: date -- 格式化一个本地时间/日期 gmdate -- 格式化一个 GMT/UTC 日期/时间,返回的是 ...

  6. Hibernate中HQL函数汇总及获取当前时间进行比较举例

    在很多时候,我们负责的项目中,在数据访问层(DAO层)通常我们会使用sql语句或者hql语句,而在我们使用hql语句拼接时有时会报错,通常的原因是:我们使用了标准的sql语句,开启的确是hiberna ...

  7. js中获取时间new date()的用法 获取时间:

    获取时间: 1 var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.getF ...

  8. PHP 时间函数 date 参数详解

    time();表示的是从1970-01-01到现在共走了多少秒,不便于看,但便于计算 要找出前一天的时间就是 time()-60*60*24; 要找出前一年的时间就是 time()*60*60*24* ...

  9. 时间函数 date strtotime

    date_default_timezone_set('Asia/Shanghai');echo strtotime('Today');echo strtotime(date('Y-m-d')); 获取 ...

随机推荐

  1. ARC引用计数

    NSlog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)self)); block保环流---> ...

  2. java学习笔记 --- String类

    一.定义 就是由多个字符组成的一串数据.也可以看成是一个字符数组. 注意: 1.字符串是常量:它们的值在创建之后不能更改.为什么? 意思就是说字符串确定了,就会在常量池中生成这个字符串. 所以说它的值 ...

  3. iOS开发之数据存储之NSData

    1.概述 使用archiveRootObject:toFile:方法可以将一个对象直接写入到一个文件中,但有时候可能想将多个对象写入到同一个文件中,那么就要使用NSData来进行归档对象. NSDat ...

  4. cassandra高级操作之索引、排序以及分页

    本次就给大家讲讲cassandra的高级操作:索引.排序和分页:处于性能的考虑,cassandra对这些支持都比较简单,所以我们不能希望cassandra完全适用于我们的逻辑,而是应该将我们的逻辑设计 ...

  5. Tcl与Design Compiler (八)——DC的逻辑综合与优化

    本文属于原创手打(有参考文献),如果有错,欢迎留言更正:此外,转载请标明出处 http://www.cnblogs.com/IClearner/  ,作者:IC_learner 对进行时序路径.工作环 ...

  6. AP付款出现(-1)例外处理

    ---手工处理方法---1.根据收款编号查询事件表中的"事件ID"---2.将AP_CHECKS_ALL表中的PAYMENT_TYPE_FLAG 标记由"Q"更 ...

  7. Zookeeper-3.4.9 集群搭建

    这里用了三台主机,系统为CentOS7 1.修改hosts #vim /etc/hosts 172.50.0.31 node1 172.50.0.34 node2 172.50.0.37 node3 ...

  8. div内部元素居中

    要让div内部元素垂直居中,则给div加上此css样式: .div-vertical-middle{  height:200px;  width:304px;  line-height:50px;  ...

  9. iOS 任务的依赖操作

    -(void)dependency{ /** 假设有A.B~C三个操作,要求: 1. 3个操作都异步执行 2. 操作C依赖于操作B 3. 操作B依赖于操作A */ //创建一个队列 NSOperati ...

  10. 【webpack学习笔记(一)】流行的前端模块化工具webpack初探

    从开发文件到生产文件   有一天我突然意识到一个问题,在使用react框架搭建应用时,我使用到了sass/less,JSX模版以及ES6的语法在编辑器下进行开发,使用这些写法是可以提高开发的效率.可是 ...