简体   繁体   中英

how to get the previous 3 months with the current month in php

如何在php中获得当前月份的前3个月?

Something like this

mktime(0, 0, 0, date("m")); //it's 00:00 first day of current month
mktime(0, 0, 0, date("m")-1); //it's 00:00 first day of previous month
mktime(0, 0, 0, date("m")-2); //it's 00:00 first day 2 month before 
mktime(0, 0, 0, date("m")-3); //it's 00:00 first day 3 month before

This functions return unix timestamp, and can be formated with date function.

You will need to read about functions: date() , mktime()

This can be done by appyling the string-to-time method of PHP

$lastMonth = date('m', strtotime('last month'));
$twoMonthsAgo = date('m', strtotime('-2 months'));
$threeMonthsAgo = date('m', strtotime('-3 months'));

You can read more about strtotime here: http://www.php.net/strtotime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM