Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示本周的日期_Php_Date - Fatal编程技术网

Php 显示本周的日期

Php 显示本周的日期,php,date,Php,Date,我正在寻找一种方法来显示当前一周的日期,或者使用周数 我试过了 echo date('d/m/Y', strtotime('previous monday')); 但很明显,这在今天或将来都不起作用 有人能帮忙吗?我是个新手! 谢谢如果您想要一周的日期,可以使用此代码 $week_start = new DateTime(); $week_start->setISODate($year,$week_no); echo $week_start->format('d-M-Y'); 我

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

我试过了

echo date('d/m/Y', strtotime('previous monday'));
但很明显,这在今天或将来都不起作用

有人能帮忙吗?我是个新手!
谢谢

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

$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
echo $week_start->format('d-M-Y');
我还没有测试过它,但我想它会给你一个周一的日期,你可以从周一开始,然后再去其他日子


来源:

这是一个使用周数获取日期的函数。
First param是周数(1=一年中的第一周,模式设置为ISO),
第二个参数是周-日指数(0-6,0=周一,6=周日)
前三个参数是可选的,否则将使用当前值进行设置。
如果没有任何参数,您将以问题中使用的格式获取当前日期。
如果将第二个参数(weekday)设置为7(或更高),则将获得一周中所有天数的数组。
使用格式“H:i”,您可以看到夏令时的偏移量,例如,夏季为“01:00”,否则为“00:00”

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);
函数getWeekDate($week=-1,$weekday=-1,$year=-1,$format=“d/m/Y”,$mode=“ISO”){ //默认值为当前值 如果($week<0){$week=date(“W”);} 如果($weekday<0){$weekday=date(“w”)-1;} 如果($weekday<0){$weekday+=7;}//在星期日 如果($year<0){$year=date(“Y”);} $time=strottime(“1月1日$year”,time()); $day=日期('w',$time); 如果($mode==“ISO”){ //ISO 8601:一年中的第一周是第一个星期四所在的一周
如果($day,我会这样做。基本上找到最后一个星期天(除非目标日期是星期天),然后循环几天直到你到达星期六

<?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>';
}

下面的代码片段应该根据您输入的星期为您提供前几天的信息:

$weeks = 1;
$weekdays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
$first_day_current_year = mktime(0, 0, 0, 1, 1, date('Y', time())); // d/m/Y H:i:s => 01/01/YYYY 00:00:00
$n_weeks_after = strtotime('+ '. $weeks . ' week', $first_day_current_year); // d/m/Y H:i:s => 08/01/YYYY 00:00:00
$result = ['reference date' => date('l, d/m/Y, H:i:s', $n_weeks_after)];

foreach ($weekdays as $day) {
  $weekday = strtotime('previous ' . $day, $n_weeks_after);
  $result['previous ' . $day] = date('l, d/m/Y, H:i:s', $weekday);
}

echo '<pre>';
print_r($result);
echo '</pre>';
对于当前周,您可以将
$weeks
设置为:


你可以在这里测试:

我不确定我是否理解你的问题。你的目标是什么?你想要一周中的当前日期吗?请编辑它,它非常混乱!对不起,我不知道如何使用它,我想从周数中获取一周中每一天的日期?你输入周数,并想要获取mo、tue、we的日期d、 星期四和星期五?@Reflic-没错
Array
(
    [reference date] => Thursday, 08/01/2015, 00:00:00
    [previous monday] => Monday, 05/01/2015, 00:00:00
    [previous tuesday] => Tuesday, 06/01/2015, 00:00:00
    [previous wednesday] => Wednesday, 07/01/2015, 00:00:00
    [previous thursday] => Thursday, 01/01/2015, 00:00:00
    [previous friday] => Friday, 02/01/2015, 00:00:00
    [previous saturday] => Saturday, 03/01/2015, 00:00:00
    [previous sunday] => Sunday, 04/01/2015, 00:00:00
)
$weeks = date('W', time()); // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)