搜尋
首頁php教程php手册PHP使用strtotime获取上个月、下个月、本月的日期,strtotime上个月

PHP使用strtotime获取上个月、下个月、本月的日期,strtotime上个月

今天写程序的时候,突然发现了很早以前写的获取月份天数的函数,经典的switch版,但是获得上月天数的时候,我只是把月份-1了,估计当时太困了吧,再看到有种毛骨悚然的感觉,本来是想再处理一下的,但是一想肯定还有什么超方便的方法,于是找到了下面这个版本,做了一点小修改。

  获取本月日期:

 function getMonth($date){
   $firstday = date("Y-m-01",strtotime($date));
   $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));
   return array($firstday,$lastday);
 }

  $firstday是月份的第一天,假如$date是2014-2这样的话,$firstday就会是2014-02-01,然后根据$firstday加一个月就是2014-03-01,再减一天就是2014-02-28,用date()和strtotime()真是太方便了。

  获取上月日期:

 function getlastMonthDays($date){
   $timestamp=strtotime($date);
   $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
   $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
   return array($firstday,$lastday);
 }

  上月日期需要先获取一个时间戳,然后在月份上-1就OK了,超智能的date()会把2014-0-1这种东西转换成2013-12-01,太爽了。

  获取下月日期:

 function getNextMonthDays($date){
   $timestamp=strtotime($date);
   $arr=getdate($timestamp);
   if($arr['mon'] == 12){
     $year=$arr['year'] +1;
     $month=$arr['mon'] -11;
     $firstday=$year.'-0'.$month.'-01';
     $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
   }else{
     $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)+1).'-01'));
     $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
   }
   return array($firstday,$lastday);
 }

  下月日期的代码看起来比较长一点,因为date()转不了类似2014-13-01这种东西,它会直接回到1970,所以前面需要处理一下12月的问题,除了12月就直接月份+1就OK啦。

  总得来说,还是很方便的,日期函数太强大了。

最后简单介绍下strtotime的用法

获取指定日期的unix时间戳

  strtotime("2009-1-22") 示例如下:
  echo strtotime("2009-1-22")
  结果:1232553600
  说明:返回2009年1月22日0点0分0秒时间戳

获取英文文本日期时间

  示例如下:
  便于比较,使用date将当时间戳与指定时间戳转换成系统时间

  (1)打印明天此时的时间戳strtotime("+1 day")
  当前时间:
  echo date("Y-m-d H:i:s",time())
  结果:2009-01-22 09:40:25
  指定时间:
  echo date("Y-m-d H:i:s",strtotime("+1 day"))
  结果:2009-01-23 09:40:25

  (2)打印昨天此时的时间戳strtotime("-1 day")
  当前时间:
  echo date("Y-m-d H:i:s",time())
  结果:2009-01-22 09:40:25
  指定时间:
  echo date("Y-m-d H:i:s",strtotime("-1 day"))
  结果:2009-01-21 09:40:25

  (3)打印下个星期此时的时间戳strtotime("+1 week")
  当前时间:
  echo date("Y-m-d H:i:s",time())
  结果:2009-01-22 09:40:25
  指定时间:
  echo date("Y-m-d H:i:s",strtotime("+1 week"))
  结果:2009-01-29 09:40:25

  (4)打印上个星期此时的时间戳strtotime("-1 week")
  当前时间:
  echo date("Y-m-d H:i:s",time())
  结果:2009-01-22 09:40:25
  指定时间:
  echo date("Y-m-d H:i:s",strtotime("-1 week"))
  结果:2009-01-15 09:40:25

  (5)打印指定下星期几的时间戳strtotime("next Thursday")
  当前时间:
  echo date("Y-m-d H:i:s",time())
  结果:2009-01-22 09:40:25
  指定时间:
  echo date("Y-m-d H:i:s",strtotime("next Thursday"))
  结果:2009-01-29 00:00:00

  (6)打印指定上星期几的时间戳strtotime("last Thursday")
  当前时间:
  echo date("Y-m-d H:i:s",time())
  结果:2009-01-22 09:40:25
  指定时间:
  echo date("Y-m-d H:i:s",strtotime("last Thursday"))
  结果:2009-01-15 00:00:00

以上示例可知,strtotime能将任何英文文本的日期时间描述解析为Unix时间戳,我们结合mktime()或date()格式化日期时间获取指定的时间戳,实现所需要的日期时间。

您可能感兴趣的文章:

  • PHP strtotime函数详解
  • php 深入理解strtotime函数的使用详解
  • PHP时间戳 strtotime()使用方法和技巧
  • php使用strtotime和date函数判断日期是否有效代码分享
  • PHP下获取上个月、下个月、本月的日期(strtotime,date)
  • php使用date和strtotime函数输出指定日期的方法
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!