<?
//日期特殊操作控制类
//宇凡 8月15日
?>
<?
//获取"yyyy-mm-dd"格式的当前时间
function nowdate()
{
$result = date("y")."-".date("m")."-".date("d");
return $result;
}
//获取"yyyy-mm-dd hh:ii:ss"格式的当前时间
function nowdatetime()
{
$result = date("y")."-".date("m")."-".date("d")." ".date("h").":".date("i").":".date("s");
return $result;
}
//从"yyyy-mm-dd"格式的时间中提取年
function getyear($date)
{
$pos = strpos($date,"-");
$result = "";
for ($i=0;$i<$pos;$i++)
{
$result = $result.$date[$i];
}
$month = getmonth($date);
$day = getday($date);
if (!checkdate($month,$day,$result))
{
$result = false;
}
return $result;
}
//从"yyyy-mm-dd"格式的时间中提取月
function getmonth($date)
{
$pos1 = strpos($date,"-");
$pos2 = strrpos($date,"-");
$result = "";
for ($i=$pos1+1;$i<$pos2;$i++)
{
$result = $result.$date[$i];
}
$year = getyear($date);
$day = getday($date);
if (!checkdate($result,$day,$year))
{
$result = false;
}
return $result;
}
//从"yyyy-mm-dd"格式的时间中提取日
function getday($date)
{
$pos = strrpos($date,"-");
$result = "";
for ($i=$pos+1;$i<10;$i++)
{
$result = $result.$date[$i];
}
$year = getyear($date);
$month = getday($date);
if (!checkdate($month,$result,$year))
{
$result = false;
}
return $result;
return $result;
}
//检查闰年,返回布尔值
function checkleap($year)
{
$result = (($year % 4 == 0) && (($year % 100 <> 0) || ($year % 400 == 0)));
return $result;
}
//在当前日期上加天数 返回"yyyy-mm-dd"格式的时间
function addday($offset)
{
$year=date("y");
$month=date("m");
$day=date("d");
for ($i=0;$i<$offset;$i++)
{
switch ($month) {
case 1:
{
if ($day==31) {
$day = 1;
$month = 2;
}
else {
$day=$day + 1;
}
}
break;
case 2:
{
if (checkleap($year)) {
if ($day == 29) {
$day = 1;
$month = 3;
}
else {
$day=$day + 1;
}
}
else {
if ($day == 28) {
$day = 1;
$month = 3;
}
else {
$day=$day + 1;
}
}
}
break;
case 3:
{
if ($day==31) {
$day = 1;
$month = 4;
}
else {
$day=$day + 1;
}
}
break;
case 4:
{
if ($day==30) {
$day = 1;
$month = 5;
}
else {
$day=$day + 1;
}
}
break;
case 5:
{
if ($day==31) {
$day = 1;
$month = 6;
}
else {
$day=$day + 1;
}
}
break;
case 6:
{
if ($day==30) {
$day = 1;
$month = 7;
}
else {
$day=$day + 1;
}
}
break;
case 7:
{
if ($day==31) {
$day = 1;
$month = 8;
}
else {
$day=$day + 1;
}
}
break;
case 8:
{
if ($day==31) {
$day = 1;
$month = 9;
}
else {
$day=$day + 1;
}
}
break;
case 9:
{
if ($day==30) {
$day = 1;
$month = 10;
}
else {
$day=$day + 1;
}
}
break;
case 10:
{
if ($day==31) {
$day = 1;
$month = 11;
}
else {
$day=$day + 1;
}
}
break;
case 11:
{
if ($day==30) {
$day = 1;
$month = 12;
}
else {
$day=$day + 1;
}
}
break;
case 12:
{
if ($day==31) {
$day = 1;
$month = 1;
$year = $year + 1;
}
else {
$day=$day + 1;
}
}
}
}
$result=$year."-".$month."-".$day;
return $result;
}
//在当前日期上减天数 返回"yyyy-mm-dd"格式的时间
function abateday($offset)
{
$year=date("y");
$month=date("m");
$day=date("d");
for ($i=0;$i<$offset;$i++)
{
switch ($month) {
case 1:
{
if ($day==1) {
$day = 31;
$month = 12;
$year = $year - 1;
}
else {
$day=$day - 1;
}
}
break;
case 2:
{
if ($day == 1) {
$day = 31;
$month = 1;
}
else {
$day=$day - 1;
}
}
break;
case 3:
{
if ($day == 1) {
if (checkleap($year)) {
$day = 29;
}
else {
$day = 28;
}
$month = 2;
}
else {
$day=$day - 1;
}
}
break;
case 4:
{
if ($day == 1) {
$day = 31;
$month = 3;
}
else {
$day=$day - 1;
}
}
break;
case 5:
{
if ($day == 1) {
$day = 30;
$month = 4;
}
else {
$day=$day - 1;
}
}
break;
case 6:
{
if ($day == 1) {
$day = 31;
$month = 5;
}
else {
$day=$day - 1;
}
}
break;
case 7:
{
if ($day == 1) {
$day = 30;
$month = 6;
}
else {
$day=$day - 1;
}
}
break;
case 8:
{
if ($day == 1) {
$day = 31;
$month = 7;
}
else {
$day=$day - 1;
}
}
break;
case 9:
{
if ($day == 1) {
$day = 31;
$month = 8;
}
else {
$day=$day - 1;
}
}
break;
case 10:
{
if ($day == 1) {
$day = 30;
$month = 9;
}
else {
$day=$day - 1;
}
}
break;
case 11:
{
if ($day == 1) {
$day = 31;
$month = 10;
}
else {
$day=$day - 1;
}
}
break;
case 12:
{
if ($day == 1) {
$day = 30;
$month = 11;
}
else {
$day=$day - 1;
}
}
}
}
$result=$year."-".$month."-".$day;
return $result;
}
//获取当前周第一天 返回"yyyy-mm-dd"格式的时间
function firstdayofthisweek()
{
$offset=date("w");
if ($offset==0)
{
$offset=7;
}
$offset--;
$result = abateday($offset);
return $result;
}
//获取当前周最后一天 返回"yyyy-mm-dd"格式的时间
function lastdayofthisweek()
{
$offset=date("w");
if ($offset==0)
{
$offset=7;
}
$offset = 7 - $offset;
$result = addday($offset);
return $result;
}
//获取指定月的天数
function daysamountofthismonth($month,$year)
{
switch ($month) {
case 1:
$result = 31;
break;
case 2:
{
if (checkleap($year)) {
$result = 29;
} else {
$result = 28;
}
break;
}
case 3:
$result = 31;
break;
case 4:
$result = 30;
break;
case 5:
$result = 31;
break;
case 6:
$result = 30;
break;
case 7:
$result = 31;
break;
case 8:
$result = 31;
break;
case 9:
$result = 30;
break;
case 10:
$result = 31;
break;
case 11:
$result = 30;
break;
case 12:
$result = 31;
break;
}
return $result;
}
?>
新闻热点
疑难解答