簡體   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僅給我一個月名,即“ March”。

我如何以MAR-2015格式獲取上個月的年份?

請參見日期功能選項:

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

您在日期函數中缺少“ 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

您可以使用php函數strtotime() ,即:

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

輸出:

MAR-2015

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM