Heim  >  Artikel  >  Backend-Entwicklung  >  php取本周、本月的第一天与最后一天

php取本周、本月的第一天与最后一天

WBOY
WBOYOriginal
2016-07-25 08:57:001717Durchsuche
  1. echo '本周第一天(星期日为一周开始):'.date('Y-m-d', time()-86400*date('w')).'
    ';
  2. echo '本周第一天(星期一为一周开始):'.date('Y-m-d', time()-86400*date('w')+(date('w')>0?86400:-6*86400)).'
    ';
  3. echo '本月第一天:'.date('Y-m-d', mktime(0,0,0,date('n'),1,date('Y'))).'
    ';
  4. echo '本月最后一天:'.date('Y-m-d', mktime(0,0,0,date('n'),date('t'),date('Y'))).'
    ';
复制代码

2,PHP DATE 取得当月的第一天和最后一天! echo date('Y-m-01',time()).'----'.date('Y-m-t',time())

3,PHP时间函数 给定日期的下个月第一天 当月第一天

代码: function GetPurMonth($date){//获取指定日期上个月的第一天和最后一天。

  1. $time=strtotime($date);
  2. $firstday=date('Y-m-01',strtotime(date('Y',$time).'-'.(date('m',$time)-1).'-01'));
  3. $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
  4. return array($firstday,$lastday);
  5. }
复制代码

问题出现了。 比如获取第一天,上面这种使用(date('m',$time)-1);万一要是获取给定日期的上上个月呢。 难道要直接减2?那万一要是给定的日期是1月份呢。难道最终会成为 date('Y-m-01', strtotime('2012--1-01')) ?????这明显是个错误 的时间格式。 所以这种函数不适合扩展使用。

查询PHP函数库,找到一个函数 mktime();

mktime() 定义和用法 mktime() 函数返回一个日期的 Unix 时间戳。 参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。 参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。 语法 mktime(hour,minute,second,month,day,year,is_dst)

例子: mktime() 函数对于日期运算和验证非常有用。它可以自动校正越界的输入; 这是什么意思。自动校正越界输入。对头。我们就是要这个功能。下面看下实例

  1. echo date('Y-m-d', strtotime('2012--2-1'));
  2. echo '
  3. ';
  4. echo date('Y-m-d', mktime(0,0,0,-2,1,2012));
复制代码

要输出2012年1月1号前2个月的日期。 即 2011年10月1号。 1970-01-01 2011-10-01 结论: 第一个函数出错了。显示了时间戳为0的时间。 而第二个函数让我们得到了想要的时间。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn