第一个星期六选定的一个月和一年 [英] First saturday for a selected month and year
本文介绍了第一个星期六选定的一个月和一年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何找到给定月份和年份(日历选择的月份和年份)的第一个星期六?我正在使用PHP。
How could I find the first Saturday for a given month and year (month & year selected by a calender)? I am using PHP.
推荐答案
使用此功能:
<?php
/**
*
* Gets the first weekday of that month and year
*
* @param int The day of the week (0 = sunday, 1 = monday ... , 6 = saturday)
* @param int The month (if false use the current month)
* @param int The year (if false use the current year)
*
* @return int The timestamp of the first day of that month
*
**/
function get_first_day($day_number=1, $month=false, $year=false)
{
$month = ($month === false) ? strftime("%m"): $month;
$year = ($year === false) ? strftime("%Y"): $year;
$first_day = 1 + ((7+$day_number - strftime("%w", @mktime(0,0,0,$month, 1, $year)))%7);
return @mktime(0,0,0,$month, $first_day, $year);
}
// this will output the first saturday of january 2007 (Sat 06-01-2007)
echo strftime("%a %d-%m-%Y", get_first_day(6, 1, 2007));
?>
学分: php.net
:另一个简短的方法:
使用strtotime()这样:
Use strtotime() like this:
$month = 1;
$year = 2007;
echo date('d/M/Y',strtotime('First Saturday '.date('F o', @mktime(0,0,0, $month, 1, $year))));
输出:
06/Jan/2007
这篇关于第一个星期六选定的一个月和一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文