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 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services