每个月的第一天

发布于 2024-12-08 17:39:57 字数 148 浏览 6 评论 0原文

如果我从当前日期开始,如何获得每个月的第一个星期五?

我正在考虑使用 $date->get(Zend::WEEKDAY) 并将其与星期五进行比较,然后与 DAY 进行比较,并检查它是否小于或等于 7。然后添加 1 个月。

一定有更简单的东西吗?

If I start with the current date, how can I get the first Friday of each month?

I was thinking about using $date->get(Zend::WEEKDAY) and comparing that to Friday and then with DAY and checking if it is less than or equal to 7. Then adding 1 month onto it.

There must be something simpler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡莣 2024-12-15 17:39:57

怎么样

$firstFridayOfOcober = strtotime('first friday of october');

或者把它变成一个方便的函数:-

 /**
 * Returns a timestamp for the first friday of the given month
 * @param string $month
 * @return type int
 */
function firstFriday($month)
{
    return strtotime("first friday of $month");
}

你可以像这样将它与 Zend_Date 一起使用:-

$zDate = new Zend_Date();
$zDate->setTimestamp(firstFriday('october'));

然后 Zend_Debug::dump($zDate->toString()); 将产生:-

string '7 Oct 2011 00:00:00' (length=19)

I'我想说这要简单得多:)

经过更多思考后进行编辑:

更通用的函数可能对您更有用,所以我建议使用这个:-

/**
 * Returns a Zend_Date object set to the first occurence
 * of $day in the given $month.
 * @param string $day
 * @param string $month
 * @param optional mixed $year can be int or string
 * @return type Zend_Date
 */
function firstDay($day, $month, $year = null)
{
    $zDate = new Zend_Date();
    $zDate->setTimestamp(strtotime("first $day of $month $year"));
    return $zDate;
}

这些天我的首选方法是延长PHP 的 DateTime 对象:-

class MyDateTime extends DateTime
{
    /**
    * Returns a MyDateTime object set to 00:00 hours on the first day of the month
    * 
    * @param string $day Name of day
    * @param mixed $month Month number or name optional defaults to current month
    * @param mixed $year optional defaults to current year
    * 
    * @return MyDateTime set to last day of month
    */
    public function firstDayOfMonth($day, $month = null, $year = null)
    {
        $timestr = "first $day";
        if(!$month) $month = $this->format('M');
        $timestr .= " of $month $year";
        $this->setTimestamp(strtotime($timestr));
        $this->setTime(0, 0, 0);
        var_dump($this);
    }
}
$dateTime = new MyDateTime();
$dateTime->firstDayOfMonth('Sun', 'Jul', 2011);

给出:-

object(MyDateTime)[36]
  public 'date' => string '2011-07-03 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

How about

$firstFridayOfOcober = strtotime('first friday of october');

Or turn it into a handy function:-

 /**
 * Returns a timestamp for the first friday of the given month
 * @param string $month
 * @return type int
 */
function firstFriday($month)
{
    return strtotime("first friday of $month");
}

You could use this with Zend_Date like this:-

$zDate = new Zend_Date();
$zDate->setTimestamp(firstFriday('october'));

Then Zend_Debug::dump($zDate->toString()); will produce :-

string '7 Oct 2011 00:00:00' (length=19)

I'd say that's a lot simpler :)

Edit after some more thought:

A more generalised function may be of more use to you, so I'd suggest using this:-

/**
 * Returns a Zend_Date object set to the first occurence
 * of $day in the given $month.
 * @param string $day
 * @param string $month
 * @param optional mixed $year can be int or string
 * @return type Zend_Date
 */
function firstDay($day, $month, $year = null)
{
    $zDate = new Zend_Date();
    $zDate->setTimestamp(strtotime("first $day of $month $year"));
    return $zDate;
}

These days my preferred method is to extend PHP's DateTime object:-

class MyDateTime extends DateTime
{
    /**
    * Returns a MyDateTime object set to 00:00 hours on the first day of the month
    * 
    * @param string $day Name of day
    * @param mixed $month Month number or name optional defaults to current month
    * @param mixed $year optional defaults to current year
    * 
    * @return MyDateTime set to last day of month
    */
    public function firstDayOfMonth($day, $month = null, $year = null)
    {
        $timestr = "first $day";
        if(!$month) $month = $this->format('M');
        $timestr .= " of $month $year";
        $this->setTimestamp(strtotime($timestr));
        $this->setTime(0, 0, 0);
        var_dump($this);
    }
}
$dateTime = new MyDateTime();
$dateTime->firstDayOfMonth('Sun', 'Jul', 2011);

Gives:-

object(MyDateTime)[36]
  public 'date' => string '2011-07-03 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文