CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Web 开发 >  PHP

求助,日期问题

楼主HSK007(Where is the SUN)2003-12-01 18:55:36 在 Web 开发 / PHP 提问

大虾,怎样求得当前日期所在周的周一和周五的日期?  
  例如当前日期2004-1-1  
  周一2003-12-29  
  周五2004-1-2  
  PleaseeeeeeeeeeeeeeeeeeeeeeeeeeeeeHelppppppppppppppppppppppp!!!!  
  问题点数:50、回复次数:5Top

1 楼ustcfrank(勇敢的心)回复于 2003-12-01 19:11:57 得分 0

1.求今天周几  
  2.求本周某天的日期Top

2 楼dlmu6307(玩具狗)回复于 2003-12-01 19:53:42 得分 15

function   LastWeekday()   {  
  $tmpyear=date(Y);  
  $tmpmonth=date(m);  
  $tmpday=date(d);  
  $temp   =   date(w);   //   the   day   of   the   week  
  if   ($temp==0   )   $temp=7;   //   0   is   sunday  
  //   每个星期以星期1为开始,星期天为结束,所以是为了获得所在这个星期的开始和结束  
  if   ($tmpday   <   $temp)   {//   那就是说上个开始肯定是上个月的,比如今天是1号却是星期天,那上个开始肯定是上个月  
  $lastmonth   =   $tmpmonth   -   1;  
  if   ($lastmonth   <   1)   {   //   也就是说上个月是12月咯~  
  $lastmonth   =   12;  
  $lastyear   =   $tmpyear   -   1;   //这里就不再处理了~  
  }   else   {   //  
  $lastmonth   =   sprintf("%02d",$lastmonth);   //    
  $lastyear   =   $tmpyear;  
  }  
   
  $lastmonthdays   =   monthdays($lastmonth,   $lastyear);  
  $lastday   =   $lastmonthdays   +   $tmpday   -   $temp   +   1;   //   比如今天是8月2号,星期5,然后31+2-5   +   1   =   29   也就是说上个月29是星期1    
  //   再比如2002.9.1是星期天,那么31+1-7+1   =   26~~:)  
  }   else   {   //   那上个开始肯定就是这个月的咯,如果都是7的话就是第一天咯~比如今天是1号是星期1,那今天就是开始  
  $lastmonth   =   sprintf("%02d",$tmpmonth);   //    
  $lastyear   =   $tmpyear;  
  $lastday   =   $tmpday   -   $temp   +   1;   //比如9.2是星期1,那么2-1+1   =   2   就是今天咯~  
  }  
  $lastday   =   sprintf("%02d",$lastday);  
  $lastweek   =   "$lastyear$lastmonth$lastday";  
  return   $lastweek;  
  }  
  function   NextWeekday()   {  
  $tmpyear=date(Y);  
  $tmpmonth=date(m);  
  $tmpday=date(d);  
  $temp   =   date(w);   //   the   day   of   the   week  
  if   ($temp==0   )   $temp=7;   //   0   is   sunday  
  //   那今天离这个星期的结束还有天数  
  $temp2   =   7   -   $temp;  
  $nextday   =   $tmpday   +   $temp2;  
  $monthdays   =   monthdays($tmpmonth,   $tmpyear);   //这个月的天数  
  if   ($nextday   >   $monthdays)   {   //   也就是说在下个月  
  $nextmonth   =   $tmpmonth   +   1;  
  if   ($nextmonth   >   12)   {  
  $nextmonth   =   sprintf("%02d",1);    
  $nextyear   =     $tmpyear   +   1;  
  }   else   {  
  $nextmonth   =   sprintf("%02d",$nextmonth);    
  $nextyear   =     $tmpyear;  
  }  
  $nextday   =   $nextday   -   $monthdays;   //   如8.30是星期5,那32-31   =   1  
  }   else   {   //     也就是说在这个月  
  $nextyear   =     $tmpyear;  
  $nextmonth   =   sprintf("%02d",$tmpmonth);    
  $nextday   =   $nextday;   //   如8.20是星期2,那就是25  
  }  
  $nextday   =   sprintf("%02d",$nextday);  
  $nextweek   =   "$nextyear$nextmonth$nextday";  
  return   $nextweek;  
  }Top

3 楼ustcfrank(勇敢的心)回复于 2003-12-01 22:21:47 得分 15

<?php  
   
  function   day_in_week($num)  
  {  
  switch($num%7)  
  {  
  case   0:  
  return   '日';  
          break;  
  case   1:  
  return   '一';  
          break;  
  case   2:  
  return   '二';  
          break;  
  case   3:  
  return   '三';  
          break;  
  case   4:  
  return   '四';  
          break;  
  case   5:  
  return   '五';  
          break;  
  case   6:  
  return   '六';  
          break;  
  default:  
  return   'error';  
          break;  
  }  
  }  
   
   
  $today_in_week=date('w');  
   
  echo   '今天是:'.date('Y').'年'.date('m').'月'.date('j').'日   星期'.day_in_week($today_in_week);  
   
   
  function   get_date_by_weekdays($dayinweek)  
  {  
  echo   '<p>';  
  $today_in_week=date('w');  
  $num=$dayinweek-$today_in_week;  
  $timestamp=strtotime($num."   days");  
  $in_week=date('w',$timestamp);  
  echo     '星期'.day_in_week($in_week).date('Y',$timestamp).'年'.date('m',$timestamp).'月'.date('j',$timestamp).'日';  
  }  
   
   
  get_date_by_weekdays(3);     //得到本周三的日期  
  get_date_by_weekdays(5);     //得到本周五的日期  
  get_date_by_weekdays(0);     //得到周日的日期  
  get_date_by_weekdays(7);     //得到周日的日期  
   
  ?>Top

4 楼xuzuning(唠叨)回复于 2003-12-02 08:36:40 得分 20

<?php  
  echo   $w   =   date("w");   //得到今天是周几  
  echo   $w1   =   date("Y-m-d",strtotime((1-$w)."   day"));   //得到周一的日期  
  echo   $w5   =   date("Y-m-d",strtotime((5-$w)."   day"));   //得到周五的日期  
  ?>Top

5 楼HSK007(Where is the SUN)回复于 2003-12-02 09:14:23 得分 0

谢谢,大家了!!!!!!!!!!!!!!!!!Top

相关问题

  • 日期
  • 日期?
  • 日期
  • 日期相减得日期
  • 日期比较
  • 日期相减?
  • 日期计算
  • 日期判断
  • DBGrid与日期
  • 日期问题

关键词

  • date
  • nextday
  • nextmonth
  • tmpyear
  • 日期
  • tmpmonth
  • nextyear
  • 星期
  • weekdays
  • timestamp

得分解答快速导航

  • 帖主:HSK007
  • dlmu6307
  • ustcfrank
  • xuzuning

相关链接

  • Web开发类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo