dongzhang1864 2013-05-05 03:39
浏览 112
已采纳

获取前12个月中指定月份的年份?

I'm trying:

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

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.

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

  • 写回答

2条回答 默认 最新

  • doushuzd3033 2013-05-05 04:15
    关注

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

    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.

    DEMO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计