Home > Backend Development > PHP Tutorial > 用程序输出从星期日至星期六,每天变化的只是日期而星期不变

用程序输出从星期日至星期六,每天变化的只是日期而星期不变

WBOY
Release: 2016-06-23 13:48:26
Original
820 people have browsed it

我弄了一个简单的程序,原先的意思是输出星期日至星期六,但是现在我搞的每天日期和星期都在变化,我想要的结果是这样:
2014-9-28(星期日)
2014-9-29(星期一)
2014-9-30(星期二)
2014-10-1(星期三)
2014-10-2(星期四)
2014-10-3(星期五)
2014-10-4(星期六)
到了2014-10-5(星期日)。。。。。一次类推,只有日期在变,而星期不变,这个怎么实现的呢?这个是我原先的程序,

function getWeek($day){  $days = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');  $day = explode('-',$day);  return $days[date('w',mktime(0,0,0,$day[1],$day[2],$day[0]))];}<?phpfor($i = 0;$i < 7;$i++){$day = date('Y-m-d',strtotime(''.($i+1).' day'));echo $day."(".getWeek($day).")";}?>
Copy after login


回复讨论(解决方案)

这个意思?

function getWeek($day){  $days = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');  return $days[date('w', strtotime($day))];}$date = '2014-09-29';if($w = date('w', strtotime($date))) $date = date('Y-m-d', strtotime("-$w day $date"));for($i = 0;$i < 7;$i++){$day = date('Y-m-d',strtotime("$i day $date"));echo $day."(".getWeek($day).")\n";}
Copy after login
Copy after login

这个意思?

function getWeek($day){  $days = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');  return $days[date('w', strtotime($day))];}$date = '2014-09-29';if($w = date('w', strtotime($date))) $date = date('Y-m-d', strtotime("-$w day $date"));for($i = 0;$i < 7;$i++){$day = date('Y-m-d',strtotime("$i day $date"));echo $day."(".getWeek($day).")\n";}
Copy after login
Copy after login


就是这种效果,但是版主定义的$date=‘2014-09-29’起什么作用呢?

我理解成楼主想要这周的周一到周日。。。
header("Content-Type: text/html; charset=utf-8");

$timestamp=time();
$is_return_timestamp=false;
static $cache ;
$id = $timestamp.$is_return_timestamp;
if(!isset($cache[$id])){
if(!$timestamp) $timestamp = time();
$monday_date = date('Y-m-d', $timestamp-86400*date('w',$timestamp)+(date('w',$timestamp)>0?86400:-/*6*86400*/518400));
if($is_return_timestamp){
$cache[$id] = strtotime($monday_date);
}else{
$cache[$id] = $monday_date;
}
}
$Monday=strtotime($cache[$id]);
$Tuesday=$Monday+3600*24*1;
$Wednesday=$Monday+3600*24*2;
$Thursday=$Monday+3600*24*3;
$Friday=$Monday+3600*24*2;
$Saturday=$Monday+3600*24*6;
$Sundany=$Monday+3600*24*6;
echo $cache[$id].'周一
' ;
echo date("Y-m-d",$Tuesday).'周二
';
echo date("Y-m-d",$Wednesday).'周三
';
echo date("Y-m-d",$Thursday).'周四
';
echo date("Y-m-d",$Friday).'周五
';
echo date("Y-m-d",$Saturday).'周六
';
echo date("Y-m-d",$Sundany).'周日
';



?>

$Saturday=$Monday+3600*24*5; 这地方改下。。

function getWeek($day){  $days = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');  return $days[$day];}for($i=0;$i<7;$i++){    $a=strtotime("+$i day");    $b=getWeek(date('w',$a));    $c=date('y-m-d',$a);    echo "20$c$b<br/>";}
Copy after login


2014-09-28星期日2014-09-29星期一2014-09-30星期二2014-10-01星期三2014-10-02星期四2014-10-03星期五2014-10-04星期六
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template