【问题标题】:Last June -- php relative date去年六月——php相对日期
【发布时间】:2014-07-21 19:24:25
【问题描述】:

我需要在 6 月的最后一个月发表讲话。我怎么能做到这一点? 假设今天是 2014 年 9 月 1 日,我需要相对地解决 2014 年 6 月 1 日。我确实试过...

$dt = new DateTime('01-09-2014', new DateTimeZone('UTC')); $dt->modify('June last year'); echo $dt->format('d F Y');

但它给了我 2013 年 6 月 1 日,而不是 2014 年 6 月 1 日

有什么想法吗?

编辑 >>>

如果我介于 {July, Aug, Sep,...Dec} 2014 === 我需要解决 2014 年 6 月 1 日

另外,如果我在 {Jan, Feb, Mar, Apr, May} 2015 年 === 我需要解决 2014 年 6 月 1 日

无法使用 php 代码。只有“相对日期字符串”

【问题讨论】:

  • 查看here,如果您想玩弄日期,这非常有用。

标签: php date


【解决方案1】:

如果您只需要 php datetime Relative Formats 的解决方案,我认为这对于 Relative Formats 的功能逻辑是不可能的。

// 问题编辑前回答

类似的东西?

$dt = new DateTime('01-09-2014', new DateTimeZone('UTC'));

if( date('n') >= 6 )
{
  // last year june
  $dt->modify('June last year');

} else {
  // current year june
  $dt->modify('June this year');
}

echo $dt->format('d F Y');

【讨论】:

  • 这是一个函数的硬逻辑,不要认为这可以只用一个日期字符串来解决
【解决方案2】:
$dt = new DateTime ( '01-04-2014', new DateTimeZone ( 'UTC' ) );
$dt->modify ( ($dt->format ( 'm' ) < 6) ? 'June last year' : 'June this year' );
echo $dt->format ( 'd F Y' );

【讨论】:

  • 是的,但如果今天是 2015 年 1 月 1 日,那么我还需要填写去年 6 月的地址,即 2014 年 6 月 1 日。实际上我需要这个地址来解决“上一财政年度的结束月份”。
  • 我改变了它,所以它可以在去年六月得到你,但我不知道你是否以及如何在没有任何 php 代码的情况下做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 2021-03-20
相关资源
最近更新 更多