gpt4 book ai didi

zend-framework - 每个月的第一天

转载 作者:行者123 更新时间:2023-12-04 06:37:16 25 4
gpt4 key购买 nike

如果我从当前日期开始,我怎样才能得到每个月的第一个星期五?

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

一定有更简单的东西吧?

最佳答案

怎么样

$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)

我会说这要简单得多:)

深思熟虑后编辑:

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

/**
* 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 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);

给予:-

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)

关于zend-framework - 每个月的第一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7699529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com