获取过去12个月中指定月份的年份? [英] Get the year of a specified month in previous 12 months?

查看:105
本文介绍了获取过去12个月中指定月份的年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试:

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

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

But it's not returning a date. In use, the month will be selected by a user from a dropdown box. However, I have tried typing 'December' in place of the variable directly and it still doesn't work.

我希望它可以返回2012年12月1日

I'm expecting it to return "1st Dec 2012".

推荐答案

鉴于您的代码 $ m 是当前月份文字格式,您可以使用以下内容:

Given your code that $m is the current month in text format, you can use something like:

$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);

这样做是将月份转换为数值,并存储字符串值。然后,根据当前年份是否小于当前时间,将$ code> $ c 分配到当年的月份或上一个月的月份邮票。因此,如果当前时间戳是 6月1日上午1点,则仍然会选择当前年份。

What this does is it converts the month to a numerical value and stores in place of string value. Then it assigns $c to either the month of the current year, or the month of the previous year, depending on if the month of the current year is less than the current time stamp. So if the current timestamp is June 1st at 1am, then the current year will still be selected.

演示

这篇关于获取过去12个月中指定月份的年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆