| 
	
 | 
 Posted by Gucci on 06/25/26 11:56 
<?php 
/** 
 * check ine year is a leap year, and return the month day array 
 * 
 * @param int $year **the year must bigger than zero** 
 * @return array 
 */ 
function is_leap_year($year){ 
    $year=floor($year); 
    if ($year<=0) { 
    	return false; 
    } 
    $flag = false;//leap year flag 
 
    /* 
     * check the year 
     */ 
    if ($year%100==0) { 
    	if ($year%400==0) { 
    		$flag = true; 
    	} 
    } 
    else { 
        if ($year%4==0) { 
        	$flag = true; 
        } 
    } 
    $mon[0] = $flag;//leap year flag 
    $mon[1] = 31; 
    $mon[2] = $flag?29:28; 
    $mon[3] = 31; 
    $mon[4] = 30; 
    $mon[5] = 31; 
    $mon[6] = 30; 
    $mon[7] = 31; 
    $mon[8] = 31; 
    $mon[9] = 30; 
    $mon[10] = 31; 
    $mon[11] = 30; 
    $mon[12] = 31; 
    return $mon; 
} 
 
/** 
 * read a datetime string and explode it in to an array 
 * string format: YY-M-D h:m:s 
 * 
 * @param string       $datetime 
 * @return array 
 */ 
function get_date_time($datetime){ 
    $current = explode(' ', $datetime); 
    $date = $current[0]; 
    $time = $current[1]; 
    $date_tmp = explode('-',$date); 
    $time_tmp = explode(':',$time); 
    $current['year'] = $date_tmp[0]+0; 
    $current['month'] = $date_tmp[1]+0; 
    $current['day'] = $date_tmp[2]+0; 
    $current['hour'] = $time_tmp[0]+0; 
    $current['minute'] = $time_tmp[1]+0; 
    $current['second'] = $time_tmp[2]+0; 
    return $current; 
} 
 
/** 
 * calculate the datetime 
 * 
 * 
 * @param string       $datetime    //string format: YY-M-D h:m:s 
 * @param array        $diff        //time stamp 
 *        array('year'=>int, 'month'=>int, 'day'=>int, 
 *              'hour'=>int, 'minute'=>int,'second'=>int) 
 * @return string                   //result string format: YYYY-MM-DD 
hh:mm:ss 
 */ 
function date_diff($datetime, $diff){ 
    $current = get_date_time($datetime);      //get the init time stamp 
    if($curent['year']<1){ 
        return false; 
    } 
    $mon = is_leap_year($current['year']);     // get the current month 
days 
 
    /* 
     * init 
     */ 
    $second_tmp = $current['second']+$diff['second']; 
    $minute_tmp = $current['minute']+$diff['minute']; 
    $hour_tmp = $current['hour']+$diff['hour']; 
    $year_tmp = $current['year']+$diff['year']; 
    $month_tmp = $current['month']+$diff['month']; 
    $day_tmp=$current['day']+$diff['day']; 
 
    /* 
     * convert the TIME stamp into seconds 
     */ 
    $hour_tmp = $hour_tmp*60*60; 
    $minute_tmp = $minute_tmp*60; 
    $second_tmp = $hour_tmp+$minute_tmp+$second_tmp; 
    //convert the seconds into (day + second), the second couldn't be 
negative 
    if ($second_tmp<0) { 
        $day = intval($second_tmp/(24*60*60))-1; 
    } 
    else { 
        $day = intval($second_tmp/(24*60*60)); 
    } 
    $day_tmp=$current['day']+$diff['day']+$day; 
    $second_tmp = $second_tmp-($day*24*60*60); 
 
    /* 
     * convert the seconds into h:m:s 
     */ 
    $hour_tmp = intval($second_tmp/(60*60)); 
    $second_tmp = $second_tmp-($hour_tmp*60*60); 
    $minute_tmp = intval($second_tmp/(60)); 
    $second_tmp = $second_tmp-($minute_tmp*60); 
 
    /* 
     * month caculate 
     */ 
    if ($month_tmp<0) { 
        while ($month_tmp<1) { 
        	$year_tmp--; 
        	$month_tmp+=12; 
        } 
    } 
    else { 
        while ($month_tmp>12) { 
        	$year_tmp++; 
        	$month_tmp-=12; 
        } 
    } 
 
    /* 
     * day caculate 
     */ 
    if ($day_tmp<0) { 
        while ($day_tmp<1) { 
        	$month_tmp--; 
        	if ($month_tmp<1) { 
        		$year_tmp--; 
        		$month_tmp=12; 
        		$mon = is_leap_year($year_tmp); 
        	} 
        	$day_tmp+=$mon[$month_tmp]; 
        } 
    } 
    else { 
        while ($day_tmp>$mon[$month_tmp]){ 
            $day_tmp-=$mon[$month_tmp]; 
            $month_tmp++; 
            if ($month_tmp>12){ 
            	$year_tmp++; 
            	$month_tmp=1; 
            	$mon = is_leap_year($year_tmp); 
            } 
        } 
    } 
    if ($year_tmp<=0)  {    return false;                   } 
    if ($month_tmp<10) { 	$month_tmp='0'.$month_tmp;      } 
    if ($day_tmp<10)   {    $day_tmp='0'.$day_tmp;          } 
    if ($hour_tmp<10)  {   	$hour_tmp='0'.$hour_tmp;        } 
    if ($minute_tmp<10){   	$minute_tmp='0'.$minute_tmp;    } 
    if ($second_tmp<10){   	$second_tmp='0'.$second_tmp;    } 
    $str_date = $year_tmp.'-'.$month_tmp.'-'.$day_tmp.' 
'.$hour_tmp.':'.$minute_tmp.':'.$second_tmp; 
    return $str_date; 
} 
 
/** 
 *  Run date_diff() 
 **/ 
$diff['year'] = -10; 
$diff['month'] = -100; 
$diff['day'] = -500; 
$diff['hour'] = -98; 
$diff['minute'] = -237; 
$diff['second'] = -999; 
echo date_diff('2000-1-1 0:0:0',$diff)."\n"; 
echo date("Y-m-d H:i:s",mktime(0-98, 00-237, 0-999, 1-100,  1-500, 
2000-10))."\n"; 
echo "\n"; 
 
$diff['year'] = 0; 
$diff['month'] = 0; 
$diff['day'] = -500; 
$diff['hour'] = -98; 
$diff['minute'] = 0; 
$diff['second'] = 0; 
echo date_diff('2000-1-1 0:0:0',$diff)."\n"; 
echo date("Y-m-d H:i:s",mktime(0-98, 00-0, 0-0, 1-0,  1-500, 
2000-0))."\n"; 
?> 
 
the result 
========================================= 
1980-04-15 17:46:21 
1980-04-14 17:46:21 
 
1998-08-14 22:00:00 
1998-08-14 22:00:00 
 
please tell me what's wrong, 
thanks! 
 
Gucci Koo
 
  
Navigation:
[Reply to this message] 
 |