3

因此,为了创建日历,我想确定任何给定月份的第一个工作日是什么。我有以下代码:

    $today=date('Y-m-d');

    IF (!$_GET) {
    $now=time();
    }
    ELSE {
    $now=strtotime($_GET['month']);
    }

    // the month in question is linked through a GET form variable in the Ymd format

    $thisdaynow=date('Y-m-d', $now);

    $monthyear=date('F Y', $now);

    $thismonth=date('M', $now);

    $thisyear=date('Y', $now);

    $weekday=date('l', $now);

    $firstday = new DateTime($thisdaynow);
    $firstday->modify('first day of this month');
    $work=$firstday->format('Ymd');
    $firstweekday=date('l', $work);
    $firstdayweek=date('w', $work);

    ECHO 'Today is '.$thisdaynow.'<br />';

    ECHO 'The first day of the month was '.$work.'<br />';

    ECHO 'Today is a '.$weekday.'.<br />';

    ECHO 'The first day of this month was a '.$firstweekday.', the '.$firstdayweek.'th day of the week.<br />';

这返回:

今天是2013-05-06

该月的第一天是 20130501

今天是星期一。

这个月的第一天是星期六,一周的第六天。

这个月有 31 天。

对我做错的任何帮助将不胜感激!!!

4

2 回答 2

4
$inputMonth = '2013-05-01';
$month = date("m" , strtotime($inputMonth));
$year = date("Y" , strtotime($inputMonth));
$getdate = getdate(mktime(null, null, null, $month, 1, $year));
echo $getdate["weekday"];

出品:周三

如果问题仍然存在。问题可能在这里:

IF (!$_GET) {

应该

if (!isset($_GET['month'])) {

这样,您总是分配给当前time(),这就是为什么每月的第一天总是当月的。

http://phpfiddle.org/main/code/4ja-928

于 2013-06-05T23:31:24.867 回答
2

这段代码:

$m = 1;
$d = 1;
$y = 2013;

do {
    $time = strtotime($y.'-'.$m.'-'.$d);
    $month = date('F',$time);
    $dayOfMonth = date('l',$time);
    $totalDays = date('t',$time);
    echo 'First day of '.$month.', '.$y.' is '.$dayOfMonth.' 
          ('.$totalDays.' days in '.$month.').<br />';
} while (++$m < 13);

将显示这些结果:

First day of January, 2013 is Tuesday (31 days in January).
First day of February, 2013 is Friday (28 days in February).
First day of March, 2013 is Friday (31 days in March).
First day of April, 2013 is Monday (30 days in April).
First day of May, 2013 is Wednesday (31 days in May).
First day of June, 2013 is Saturday (30 days in June).
First day of July, 2013 is Monday (31 days in July).
First day of August, 2013 is Thursday (31 days in August).
First day of September, 2013 is Sunday (30 days in September).
First day of October, 2013 is Tuesday (31 days in October).
First day of November, 2013 is Friday (30 days in November).
First day of December, 2013 is Sunday (31 days in December).

PHP 手册中有更多信息:http: //php.net/date

于 2013-06-05T23:27:59.720 回答