2016-05-17 15:28
浏览 393


I want to compare two dates -- just the month and the year -- to see if one falls before the other. This is what I have currently:

$monthyear = date("M Y"); 
$finish = date("M Y",strtotime($row->finish));
$start = date("M Y",strtotime($row->start));
if ($start <= $monthyear and $finish >= $monthyear) {
   do some stuff..

What I'm trying to do, in plain English: If the start date is before or during the current month, and the finish date is after or during the current month, then do some stuff.

However, what is happening is that it is, e.g., October 2015 is seen as falling AFTER May 2016 (i.e., it runs what's inside the brackets if the finish date is October 2015). From what I can tell, it is comparing the months only, rather than the month + the year.

Any idea how to do make these comparisons correctly?

图片转代码服务由CSDN问答提供 功能建议

我想比较两个日期 - 只是月份和年份 - 看看是否有一个落在另一个之前 。 这就是我目前所拥有的:

  $ monthyear = date(“M Y”);  
 $ finish = date(“MY”,strtotime($ row-&gt; finish)); 
 $ start = date(“MY”,strtotime($ row-&gt; start)); 
if($ start&lt  ; = $ monthyear和$ finish&gt; = $ monthyear){

我正在尝试做什么,在 普通英语:如果开始日期在当月之前或期间,结束日期在当月或之后,那么就做一些事情。

然而,发生的事情是 例如,2015年10月被视为在2016年5月之后下降(即,如果完成日期是2015年10月,它将在括号内运行)。 据我所知,它只是比较月份而不是月份+年份。


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhan5246 2016-05-17 15:35

    Change the format to 'Y-m' that is alphabetically comparable as 2 strings

    $monthyear = date("Y-m"); 
    $finish = date("Y-m",strtotime($row->finish));
    $start = date("Y-m",strtotime($row->start));
    if ($start <= $monthyear and $finish >= $monthyear) {
       do some stuff..
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题