3
如果我从当前日期开始,我怎样才能得到每个月的第一个星期五?每个月的第一天
我正在考虑使用$ date-> get(Zend :: WEEKDAY)并将它与周五然后与DAY进行比较,并检查它是否小于或等于7.然后将1个月添加到它。
必须有更简单的东西?
如果我从当前日期开始,我怎样才能得到每个月的第一个星期五?每个月的第一天
我正在考虑使用$ 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)
感谢。 '''部分在php 5.2中工作吗? –
我看不出为什么不。试试吧,看看:)本页更新日志部分可能会有所帮助http://php.net/manual/en/function.strtotime.php – vascowhite
头脑风暴! O__O –