首頁  >  文章  >  後端開發  >  php實作取得目前月與上個月月初及月末時間戳記的方法

php實作取得目前月與上個月月初及月末時間戳記的方法

墨辰丷
墨辰丷原創
2018-05-29 17:36:474185瀏覽

這篇文章主要介紹了php獲取當前月與上個月月初及月末時間戳的方法,涉及php針對日期與時間相關判斷與操作技巧,需要的朋友可以參考下

具體如下:

目前月

<?php
$thismonth = date(&#39;m&#39;);
$thisyear = date(&#39;Y&#39;);
$startDay = $thisyear . &#39;-&#39; . $thismonth . &#39;-1&#39;;
$endDay = $thisyear . &#39;-&#39; . $thismonth . &#39;-&#39; . date(&#39;t&#39;, strtotime($startDay));
$b_time  = strtotime($startDay);//当前月的月初时间戳
$e_time  = strtotime($endDay);//当前月的月末时间戳

#上一月

<?php
$thismonth = date(&#39;m&#39;);
$thisyear = date(&#39;Y&#39;);
if ($thismonth == 1) {
 $lastmonth = 12;
 $lastyear = $thisyear - 1;
} else {
 $lastmonth = $thismonth - 1;
 $lastyear = $thisyear;
}
$lastStartDay = $lastyear . &#39;-&#39; . $lastmonth . &#39;-1&#39;;
$lastEndDay = $lastyear . &#39;-&#39; . $lastmonth . &#39;-&#39; . date(&#39;t&#39;, strtotime($lastStartDay));
$b_time = strtotime($lastStartDay);//上个月的月初时间戳
$e_time = strtotime($lastEndDay);//上个月的月末时间戳


這裡對關鍵的就是date函數中的t,它是用來取得當前月所含天數的,28天,29天,30天,31天。含有幾天,月底就是多少號。 以上就是本文的全部內容,希望對大家的學習有所幫助。

相關推薦:
怎麼透過

PHP

MySQL 插入多條資料
Php多重流程實作程式設計實例

如何透過PHP

MySQL 插入資料########### #################

以上是php實作取得目前月與上個月月初及月末時間戳記的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn