0

我正在努力:

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

但它没有返回日期。在使用中,月份将由用户从下拉框中选择。但是,我尝试直接输入“December”来代替变量,但它仍然不起作用。

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

4

2 回答 2

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 

键盘

键盘2

编辑

试试这个,这是在 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 

键盘

于 2013-05-05T03:58:18.943 回答
1

鉴于您的代码$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,则仍将选择当前年份。

演示

于 2013-05-05T04:15:49.797 回答