程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php獲取當前月與上個月月初及月末時間戳的方法

php獲取當前月與上個月月初及月末時間戳的方法

編輯:PHP綜合

本文實例講述了php獲取當前月與上個月月初及月末時間戳的方法。分享給大家供大家參考,具體如下:

當前月

<?php
$thismonth = date('m');
$thisyear = date('Y');
$startDay = $thisyear . '-' . $thismonth . '-1';
$endDay = $thisyear . '-' . $thismonth . '-' . date('t', strtotime($startDay));
$b_time  = strtotime($startDay);//當前月的月初時間戳
$e_time  = strtotime($endDay);//當前月的月末時間戳

上一月

<?php
$thismonth = date('m');
$thisyear = date('Y');
if ($thismonth == 1) {
 $lastmonth = 12;
 $lastyear = $thisyear - 1;
} else {
 $lastmonth = $thismonth - 1;
 $lastyear = $thisyear;
}
$lastStartDay = $lastyear . '-' . $lastmonth . '-1';
$lastEndDay = $lastyear . '-' . $lastmonth . '-' . date('t', strtotime($lastStartDay));
$b_time = strtotime($lastStartDay);//上個月的月初時間戳
$e_time = strtotime($lastEndDay);//上個月的月末時間戳

這裡對關鍵的就是date函數中的t,它是用來獲取當前月所含天數的,28天,29天,30天,31天。含有多少天,月底就是多少號。

PS:本站還提供了一個Unix時間戳轉換工具,包含了各種常見語言針對時間戳的操作方法,提供給大家參考:

Unix時間戳(timestamp)轉換工具:
http://tools.jb51.net/code/unixtime

更多關於PHP相關內容感興趣的讀者可查看本站專題:《php日期與時間用法總結》、《PHP數學運算技巧總結》、《PHP數組(Array)操作技巧大全》、《PHP數據結構與算法教程》、《php程序設計算法總結》、《php正則表達式用法總結》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved