簡體   English   中英

獲取前12個月中指定月份的年份?

[英]Get the year of a specified month in previous 12 months?

我正在努力:

$m = 'December';
$from = date('jS M Y', strtotime("first day of previous $m"));

但它沒有回復約會。 在使用中,用戶將從下拉框中選擇月份。 但是,我嘗試直接輸入'December'來代替變量,但它仍然不起作用。

我期待它回歸“2012年12月1日”。

嘗試這個

$m = '12'; // set here numeric form of month
echo $from = date('jS M Y', strtotime("01-".$m."-".(date("Y") -1)));

要么

$m = 'December';
echo $from = date('jS M Y', strtotime("01-".$m."-".(date("Y") -1)));

產量

1st Dec 2012 

鍵盤

Codepad2

編輯

試試這個,這是在1月和12月進行測試

$m = '01';
$new_m = date('m'); // get current month
if($m > $new_m) {
   $year = date("Y") -1;
} else {
   $year = date("Y");
}
echo $from = date('jS M Y', strtotime("01-".$m."-".$year));

產量

1st Jan 2013 

鍵盤

鑒於您的代碼$m是文本格式的當前月份,您可以使用以下內容:

$m = 'June';
$m = date('m', strtotime($m));
$c = (mktime(0,0,0,$m, 1) < time()) ? mktime(0,0,0,$m,1) : mktime(0,0,0,$m,1, date("Y") -1);
$from = date('jS M Y', $c);

這樣做是將月份轉換為數值並存儲以代替字符串值。 然后它將$c分配給當前年份或上一年的月份,具體取決於當前年份的月份是否小於當前時間戳。 因此,如果當前時間戳是June 1st at 1am ,那么仍將選擇當前年份。

DEMO

暫無
暫無

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

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