Reply to Re: i write a date calculator, but there always get a one-day-distance wrong

Your name:

Reply:


Posted by Gucci on 08/27/06 13:36

i solve the problem

<?php
function get_date_time($datetime){
if (!$datetime) {
return false;
}
$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;
}

function get_time_stamp($stamp_array){
if (!$stamp_array) {
return false;
}
$diff['year'] = $stamp_array[0];
$diff['month'] = $stamp_array[1];
$diff['day'] = $stamp_array[2];
$diff['hour'] = $stamp_array[3];
$diff['minute'] = $stamp_array[4];
$diff['second'] = $stamp_array[5];
return $diff;
}

function is_leap_year($year){
$year=floor($year);
if ($year<=0) {
return false;
}
$flag = false;

if ($year%100==0) {
if ($year%400==0) {$flag = true;}
}
else {
if ($year%4==0) {$flag = true;}
}
$mon = array($flag,31,($flag?29:28),31,30,31,30,31,31,30,31,30,31);
return $mon;
}

function date_calc($stamp,&$litter,&$bigger){
$time_name=array('second','minute','hour','day','month','year');
$time_base=array(60,60,24,null,12,null);
$position = array_search($stamp,$time_name);

$bigger_tmp = intval($litter/$time_base[$position]);
if ($bigger_tmp<0) {
$bigger_tmp--;
}
$litter = $litter-$bigger_tmp*$time_base[$position];
$bigger+=$bigger_tmp;
}

function date_diff($start,$diff_stamp){
$current = get_date_time($start);
$diff = get_time_stamp($diff_stamp);

/*
* IMPORTANT!!!
* MAKE ALL TIMESTAMP START AT ZERO
*/
$current['year']--;
$current['month']--;
$current['day']--;

$year = $current['year']+$diff['year'];
$month = $current['month']+$diff['month'];
$day = $current['day']+$diff['day'];
$hour = $current['hour']+$diff['hour'];
$minute = $current['minute']+$diff['minute'];
$second = $current['second']+$diff['second'];

date_calc('second',$second,$minute);
date_calc('minute',$minute,$hour);
date_calc('hour',$hour,$day);
date_calc('month',$month,$year);

$year++;
$month++;
$day++;

$mon = is_leap_year($year);
if ($day<0) {
while ($day<1) {
$month--;
if ($month<1) {
$year--;
$month=12;
$mon = is_leap_year($year);
}
$day+=$mon[$month];
}
}
else {
while ($day>$mon[$month]){
$day-=$mon[$month];
$month++;
if ($month>12){
$year++;
$month=1;
$mon = is_leap_year($year);
}
}
}

if ($month<10) {$month='0'.$month;}
if ($day<10) {$day='0'.$day;}
if ($hour<10) {$hour='0'.$hour;}
if ($minute<10) {$minute='0'.$minute;}
if ($second<10) {$second='0'.$second;}
return "$year-$month-$day $hour:$minute:$second";
}
?>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация