PHP 获取上月,本月,近15天,近30天日期

时间:2023-03-09 08:29:09
PHP 获取上月,本月,近15天,近30天日期
<?php
//echo $_SERVER['PHP_SELF'];
//define('ROOT_PATH',str_replace($_SERVER['PHP_SELF'],'',str_replace('\\', '/', __FILE__))); /**
* 获取统计时间
* @param $type
* 1 上月
* 2 本月
* 3 近15天
* 4 近30天
* @return array
*/
function getDateInfo($type)
{
$data = array(
array(
'firstday' => date('Ym01', strtotime('-1 month')),
'lastday' => date('Ymt', strtotime('-1 month')),
),
array(
'nowday' => date("Ymd"),
'nowday-25' => date('Ymd', strtotime('-25 day')),
'firstday' => date('Ym01', strtotime(date("Ymd"))),
'lastday' => date('Ymd', strtotime((date('Ym01', strtotime(date("Ymd")))) . " +1 month -1 day")),
),
array(
'firstday' => date('Ymd', strtotime("-15 day")),
'lastday' => date('Ymd', strtotime('-1 day')),
),
array(
'firstday' => date('Ymd', strtotime("-30 day")),
'lastday' => date('Ymd', strtotime('-1 day')),
),
);
return is_null($type) ? $data : $data[$type-1];
}
var_dump(getDateInfo(null));//获取上个月第一天与最后一天 function dateDifference($timestamp1 , $timestamp2 , $differenceFormat = '%a' )
{
$datetime1 = date_create("@$timestamp1");
$datetime2 = date_create("@$timestamp2"); $interval = date_diff($datetime1, $datetime2); return $interval->format($differenceFormat); }
echo '本月最后一天:'.date('Ymd', strtotime((date('Ym01', strtotime(date("Ymd")))) . " +1 month -1 day")).'<br>';
echo '到月底还有几天:'.dateDifference(strtotime(date('Ymd', strtotime((date('Ym01', strtotime(date("Ymd")))) . " +1 month"))),strtotime('now')).'<br>';
echo '到月底还有几天:'.(date('d', strtotime((date('Ym01', strtotime(date("Ymd")))) . " +1 month -1 day"))-date('d')).'<br>';
echo '本月已经过了几天'.date("d").'<br>';
echo strtotime('now').'<br>';
echo time().'<br>';
var_dump(date("d"));

 PHP打印本月天数:

$a_days = array();
for($i=0;$i<intval(date("d"));$i++){
$a_days[]=date('Y-m-d',strtotime("-$i day"));
}
var_dump($a_days); Array ( [0] => 2017-10-25 [1] => 2017-10-24 [2] => 2017-10-23 [3] => 2017-10-22 [4] => 2017-10-21 [5] => 2017-10-20 [6] => 2017-10-19 [7] => 2017-10-18 [8] => 2017-10-17 [9] => 2017-10-16 [10] => 2017-10-15 [11] => 2017-10-14 [12] => 2017-10-13 [13] => 2017-10-12 [14] => 2017-10-11 [15] => 2017-10-10 [16] => 2017-10-09 [17] => 2017-10-08 [18] => 2017-10-07 [19] => 2017-10-06 [20] => 2017-10-05 [21] => 2017-10-04 [22] => 2017-10-03 [23] => 2017-10-02 [24] => 2017-10-01 ) PHP 日期和时间戳互换:
echo date("Y-m-d 23:59:59").'<br>';
echo strtotime(date("Y-m-d 23:59:59")).'<br>';//日期转换为时间戳
echo date("Y-m-d H:i:s",'1509033599').'<br>';//时间戳转换为日期