/** * 獲取指定年月日的開始時間戳和結束時間戳(本地時間戳非GMT時間戳) * [1] 指定年:獲取指定年份第一天第一秒的時間戳和下一年第一天第一秒的時間戳 * [2] 指定年月:獲取指定年月第一天第一秒的時間戳和下一月第一天第一秒時間戳 * [3] 指定年月日:獲取指定年月日第一天第一秒的時間戳 * @param integer $year [年份] * @param integer $month [月份] * @param integer $day [日期] * @return array('start' => '', 'end' => '') */ function getStartAndEndUnixTimestamp($year = 0, $month = 0, $day = 0) { if(empty($year)) { $year = date("Y"); } $start_year = $year; $start_year_formated = str_pad(intval($start_year), 4, "0", STR_PAD_RIGHT); $end_year = $start_year + 1; $end_year_formated = str_pad(intval($end_year), 4, "0", STR_PAD_RIGHT); if(empty($month)) { //只設置了年份 $start_month_formated = '01'; $end_month_formated = '01'; $start_day_formated = '01'; $end_day_formated = '01'; } else { $month > 12 || $month < 1 ? $month = 1 : $month = $month; $start_month = $month; $start_month_formated = sprintf("%02d", intval($start_month)); if(empty($day)) { //只設置了年份和月份 $end_month = $start_month + 1; if($end_month > 12) { $end_month = 1; } else { $end_year_formated = $start_year_formated; } $end_month_formated = sprintf("%02d", intval($end_month)); $start_day_formated = '01'; $end_day_formated = '01'; } else { //設置了年份月份和日期 $startTimestamp = strtotime($start_year_formated.'-'.$start_month_formated.'-'.sprintf("%02d", intval($day))." 00:00:00"); $endTimestamp = $startTimestamp + 24 * 3600 - 1; return array('start' => $startTimestamp, 'end' => $endTimestamp); } } $startTimestamp = strtotime($start_year_formated.'-'.$start_month_formated.'-'.$start_day_formated." 00:00:00"); $endTimestamp = strtotime($end_year_formated.'-'.$end_month_formated.'-'.$end_day_formated." 00:00:00") - 1; return array('start' => $startTimestamp, 'end' => $endTimestamp); }
使用例子以下所示:php
<?php echo "<pre>"; $result = getStartAndEndUnixTimestamp(2016); $result1 = getStartAndEndUnixTimestamp(2016, 8); $result2 = getStartAndEndUnixTimestamp(2016, 9, 30); print_r($result); print_r($result1); print_r($result2); ?>
結果以下所示:學習
Array ( [start] => 1451577600 [end] => 1483199999 ) Array ( [start] => 1469980800 [end] => 1472659199 ) Array ( [start] => 1475164800 [end] => 1475251199 )
以上就是PHP 獲取指定年月日的開始和結束時間戳的全文介紹,但願對您學習和使用php有所幫助.orm