显示本周的日期

时间:2015-07-10 17:24:52

标签: php date

我正在寻找一种方法,可以使用周数来显示当前一周的日期或当前日期?

我试过了

echo date('d/m/Y', strtotime('previous monday'));

但显然这对今天或将来的日期不起作用

任何人都可以提供帮助 - 我是新手! 感谢

4 个答案:

答案 0 :(得分:1)

如果您想要一周的日期,可以使用此代码

$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
echo $week_start->format('d-M-Y');

我没有测试过,但我认为它会给你一个星期一的日期,你可以去旅行到其他日子。

来源:http://www.lornajane.net/posts/2011/getting-dates-from-week-numbers-in-php

答案 1 :(得分:0)

这是我要做的。基本上找到最后一个星期日(除非目标日期是星期日),然后循环直到你星期六的星期几。

<?php
//our target timestamp to get the week
// $targetDate = time(); //today
$targetDate = strtotime('1-Jan-2015'); //specific date

//if target day is not day 0 (sunday)
if(date('w', $targetDate) > 0) {
    //get the last sunday
    $sunday = strtotime('last sunday', $targetDate);
} else {
    //target day is sunday, get target date
    $sunday = $targetDate;
}

//if you want shorthand ternary statement, this would work for finding sunday. I wrote it out above so it is easier to understand:
// $sunday = date('w', $targetDate) ? strtotime('last sunday', $targetDate) : $targetDate;

//get the following saturday as our stop point
$saturday = strtotime('saturday', $sunday);

//loop over each day of the week
for(
    $currentDay = $sunday;      //start on sunday
    $currentDay <= $saturday;   //while we aren't on saturday yet
    $currentDay = strtotime('+1 day', $currentDay) //add one day after each loop
){
    //echo out current day's date
    echo date('d-M-Y', $currentDay).'<br>';
}

example

我个人更喜欢这个,因为你可以在循环中做其他的事情,比如回显特定日期的某些值。

答案 2 :(得分:-1)

这是使用周数获​​取日期的功能。
第一个参数是周数(1 =一年的第一周,模式设置为ISO),
第二个参数是工作日指数(0-6,0 =周一,6 =周日)
前三个参数是可选的,否则设置为当前值。
如果没有任何参数,您将以问题中使用的格式获得当前日期。
如果将第二个参数(工作日)设置为7(或更高),您将获得一周中所有日期的数组。
格式&#34; H:i&#34;你可以看到夏令时的偏移,例如它&#34; 01:00&#34;在夏天和#34; 00:00&#34;否则。

function getWeekDate($week=-1, $weekday=-1, $year=-1, $format="d/m/Y", $mode="ISO") {
    // default is the current
    if($week < 0) { $week = date("W"); }
    if($weekday < 0) { $weekday = date("w")-1; }
    if($weekday < 0) { $weekday+= 7; } // on sundays
    if($year < 0) { $year = date("Y"); }
    $time = strtotime("1 January $year", time());
    $day = date('w', $time);
    if($mode=="ISO") {
        // ISO 8601: first week of the year is the week with the first thursday
        if($day<=4) { $day+= 7; }
    }
    $time+= ((7*$week)+1-$day)*24*3600;
    $return = array();
    for($i=0;$i<=6;$i++) {
        $return[$i] = date($format, $time);
        $time+= 24*3600;
    }
    if(isset($return[$weekday])) {
        $return = $return[$weekday];
    }
    return $return;
}

// Examples:
echo "<br>Current Date: ".getWeekDate();
echo "<br>Monday of the Current Week: ".getWeekDate(-1,0);
echo "<br>Sunday of the Next Week: ".getWeekDate(date("W")+1,6);
echo "<br>All Days of Week 28: ".print_r(getWeekDate(28,7),true);

答案 3 :(得分:-1)

以下功能可根据您输入的一周为您提供前几天:

$weekdays = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
$result = array();

$today = date('d/m/Y', time());
$week = 1;
$result['today'] = $today;

foreach ($weekdays as $day) {
  $previous = mktime(0, 0, 0, 1, 1, date('Y', time()));
  $previous = strtotime('+' . $week . ' week', $previous);
  $weekday = strtotime('previous ' . $day, $previous);
  $result[$day] = date('d/m/Y', $weekday);
}

echo '<pre>';
print_r($result);
echo '</pre>';

从1月1日(星期四)到1月7日(星期三),输入是第1周。 输出如下:

Array ( [today] => 10/07/2015 [monday] => 05/01/2015 [tuesday] => 06/01/2015 [wednesday] => 07/01/2015 [thursday] => 01/01/2015 [friday] => 02/01/2015 [saturday] => 03/01/2015 [sunday] => 04/01/2015 )

对于本周,您可以将$week设置为:

$week = date('W', time()); // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)

您可以在此处进行测试:http://www.writephponline.com/