简体   繁体   English

PHP-从当月获取前一个月的名称

[英]PHP - Get previous month name from current month

$_POST['month_year'] = APR-2015

$mth = "April";
$pre_mth = Date("F", strtotime($mth . " last month")); 

$pre_mth gives me only month name which is 'March'. $pre_mth仅给我一个月名,即“ March”。

How can I fetch previous month-year in the format MAR-2015 ? 我如何以MAR-2015格式获取上个月的年份?

See the date function options: 请参见日期功能选项:

$pre_mth = strtoupper(date("M-Y", strtotime($mth . " last month")));
// strtoupper just to uppercase 'Mar' to 'MAR'

You were missing 'Y' within your date function 您在日期函数中缺少“ Y”

$mth="April";
$pre_mth = strtoupper(Date("M-Y", strtotime($mth . " last month")));
echo $pre_mth;// MAR-2015
$input = 'APR-2015';

$dt = new DateTime($input);
$dt->modify('last month');

$output = $dt->format('M-Y');

echo strtoupper($output);

// Output:
// MAR-2015

You can use the php function strtotime() , ie: 您可以使用php函数strtotime() ,即:

$post = "APR-2015";
echo strtoupper(date('M-Y', strtotime($post. "-1 Month")));

Output: 输出:

MAR-2015

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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