如何获得上个月和今年相对于今天,使用strtotime和date? [英] How to get previous month and year relative to today, using strtotime and date?

查看:96
本文介绍了如何获得上个月和今年相对于今天,使用strtotime和date?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



但是,请参阅以下示例。



回覆日期('Ym-d',strtotime('last month')); $ $ $ $ $ $ $ $

//输出:
2011-03-02

由于二月和三月的天数不同,这个行为是可以理解的(上面的示例代码是我所需要的,但在每个月的1号到28号之间只能正常工作100%。



所以,如何获得上个月和年份(以日期(Ym))以最优雅的方式可能的,一年中的每一天工作?最佳解决方案将基于 strtotime 参数解析。



更新。要澄清一点要求。



我有一段代码可以得到过去几个月的统计数据,但是我首先显示上个月的统计数据,然后加载其他需要几个月。这是目的。所以,在这个月份,我想知道我应该加载哪个月份,以便加载上一个月的统计资料。



我还有一个代码是时区 - 意识到(现在不是很重要),并且接受 strtotime 兼容的字符串作为输入(初始化内部日期),然后允许调整日期/时间,还使用



我知道可以用几个条件和基础数学来完成,但这真的很杂乱相比之下,例如(如果它当然正常工作):

  echo tz :: date('last month ') - >格式('Y-d')

所以,我只需要上个月年,在 strtotime 兼容的时尚。



回答(谢谢,@dnagirl):

  //今天是2011-03-30 
echo date('Ym-d',strtotime('上个月的第一天' )); //输出:2011-02-01


解决方案

查看 DateTime 类。它应该正确地进行计算,日期格式与 strttotime 兼容。如下:

  $ datestring ='2011-03-30上个月的第一天' 
$ dt = date_create($ datestring);
echo $ dt-> format('Y-m'); // 2011-02


I need to get previous month and year, relative to current date.

However, see following example.

// Today is 2011-03-30
echo date('Y-m-d', strtotime('last month'));

// Output:
2011-03-02

This behavior is understandable (to a certain point), due to different number of days in february and march, and code in example above is what I need, but works only 100% correctly for between 1st and 28th of each month.

So, how to get last month AND year (think of date("Y-m")) in the most elegant manner as possible, which works for every day of the year? Optimal solution will be based on strtotime argument parsing.

Update. To clarify requirements a bit.

I have a piece of code that gets some statistics of last couple of months, but I first show stats from last month, and then load other months when needed. That's intended purpose. So, during THIS month, I want to find out which month-year should I pull in order to load PREVIOUS month stats.

I also have a code that is timezone-aware (not really important right now), and that accepts strtotime-compatible string as input (to initialize internal date), and then allows date/time to be adjusted, also using strtotime-compatible strings.

I know it can be done with few conditionals and basic math, but that's really messy, compared to this, for example (if it worked correctly, of course):

echo tz::date('last month')->format('Y-d')

So, I ONLY need previous month and year, in a strtotime-compatible fashion.

Answer (thanks, @dnagirl):

// Today is 2011-03-30
echo date('Y-m-d', strtotime('first day of last month')); // Output: 2011-02-01

解决方案

Have a look at the DateTime class. It should do the calculations correctly and the date formats are compatible with strttotime. Something like:

$datestring='2011-03-30 first day of last month';
$dt=date_create($datestring);
echo $dt->format('Y-m'); //2011-02

这篇关于如何获得上个月和今年相对于今天,使用strtotime和date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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