Chinaunix首页 | 论坛 | 博客
  • 博客访问: 273284
  • 博文数量: 59
  • 博客积分: 1368
  • 博客等级: 中尉
  • 技术积分: 1071
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-02 06:06
文章分类

全部博文(59)

文章存档

2012年(59)

我的朋友

分类: 系统运维

2012-02-17 12:38:55

1 指定日期加天数
  1. <?php

  2. //PHP Example code to add one day,one month or one year to todays date

  3. $todayDate = date("Y-m-d");// current date
  4. echo "Today: ".$todayDate."
    "
    ;

  5. //Add one day to today
  6. $dateOneDayAdded = strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 day");

  7. echo "After adding one Day: ".date('l dS \o\f F Y', $dateOneDayAdded)."
    "
    ;

  8. //Add one month to today
  9. $dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 month");

  10. echo "After adding one month: ".date('l dS \o\f F Y', $dateOneMonthAdded)."
    "
    ;

  11. //Add one Year to today
  12. $dateOneYearAdded = strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 year");

  13. echo "After adding one Year: ".date('l dS \o\f F Y', $dateOneYearAdded)."
    "
    ;
  14. ?>
2 计算两天的时间间隔
  1. <?php

  2. $date1 = "2007-03-24";
  3. $date2 = "2009-06-26";

  4. $diff = abs(strtotime($date2) - strtotime($date1));

  5. $years = floor($diff / (365*60*60*24));
  6. $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
  7. $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

  8. printf("%d years, %d months, %d days\n", $years, $months, $days);
  9. ?>
3 计算两天之间的天数
  1. <?php
  2. function GetDays($sStartDate, $sEndDate){
  3.   // Firstly, format the provided dates.
  4.   // This function works best with YYYY-MM-DD
  5.   // but other date formats will work thanks
  6.   // to strtotime().
  7.   $sStartDate = gmdate("Y-m-d", strtotime($sStartDate));
  8.   $sEndDate = gmdate("Y-m-d", strtotime($sEndDate));

  9.   // Start the variable off with the start date
  10.   $aDays[] = $sStartDate;

  11.   // Set a 'temp' variable, sCurrentDate, with
  12.   // the start date - before beginning the loop
  13.   $sCurrentDate = $sStartDate;

  14.   // While the current date is less than the end date
  15.   while($sCurrentDate < $sEndDate){
  16.     // Add a day to the current date
  17.     $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));

  18.     // Add this new day to the aDays array
  19.     $aDays[] = $sCurrentDate;
  20.   }

  21.   // Once the loop has finished, return the
  22.   // array of days.
  23.   return $aDays;
  24. }

  25. $aDays = GetDays('5th Feb 2007', '10th Feb 2007');
  26. GetDays('2007-01-01', '2007-01-31');
  27. GetDays('19-02-2007', '25-02-2007');
  28. ?>


阅读(836) | 评论(1) | 转发(0) |
0

上一篇:php csv excel

下一篇:Ionize0.9.7 Bug 解决

给主人留下些什么吧!~~

2012-03-03 01:08:26

//时区信息
checkdate(month,day,year)                //验证日期是否存在
date_default_timezone_set("Asia/Shanghai")        //设置时区
echo date_default_timezone_get(); (PRC)                //返回时区
date_sunrise(timestamp,format,latitude,longitude,zenith,gmt_offset)        //返回日出时间
date_sunset(timestamp,format,latitude,longitude,zenith,gmt_offset)