473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

i write a date calculator, but there always get a one-day-distance wrong

<?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

Aug 26 '06 #1
7 1666
Zebrawszy my?li Gucci <ju******@gmail.comwyklepa?:

/**
* check ine year is a leap year, and return the month day array
*
* @param int $year **the year must bigger than zero**
* @return array
*/

OMG man, you should to rewrite this code...

1. try to use date("L") for leap year
2. too much of code
3. same solution for other functions....

--
~~~~~~~~~~~~~~~~~~~~~~~~~
..::[ ikciu ]::.
gg: 718845
www: e-irsa.pl
Aug 26 '06 #2
This is usually because there is the North American Daylight Savings time
built in to PHP.

Why write one yourself when there are loads already written?
Aug 26 '06 #3
has i have to calculate the datetime which beyond the unixtime on
32-bit server

Aug 26 '06 #4
Gucci wrote:
<?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;
}
<snip>

FWIW, this is one of many reasons why everyone should read K&R.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Aug 26 '06 #5
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','m onth','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";
}
?>

Aug 27 '06 #6
On Sun, 27 Aug 2006 06:36:09 -0700, Gucci wrote:
if ($year%100==0) {
if ($year%400==0) {$flag = true;}
Are you *sure* this logic is correct? I'm not... and if it is, why bother
with the check for 100 years?

Aug 28 '06 #7
Steve wrote:
On Sun, 27 Aug 2006 06:36:09 -0700, Gucci wrote:

> if ($year%100==0) {
if ($year%400==0) {$flag = true;}


Are you *sure* this logic is correct? I'm not... and if it is, why bother
with the check for 100 years?
Yes, that is correct.

It is a leap year if the year is divisible by 400, or if its divisible
by 4 and not by 100.

He needs the $year % 100 for the else clause.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 28 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dennis M. Marks | last post by:
I've been updating by date stuff at my web site using much of the information obtained here. Would you please check it for any errors. The two pages are "Yearly Calendar" and "Date Calculator"....
4
by: mwh | last post by:
Hi. If you remember, I posted Expressons Help. Now I am making a calculator with javascript. I can't get this to work: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
3
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
5
by: Steven Smith | last post by:
I was flicking through the windows accesories programs for some inspiration for todays vb challenge when I came accross the calculator program & thought that looks easy I could do that. However...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
1
by: teddymeu | last post by:
Hi Guys Dont have much experience in writing my own JavaScripts and have a problem to overcome for work They need a date calculator which displays 90 days forward from a selected date, it will...
25
by: mereba | last post by:
Hello My country Ghana is changing its currency. I want to write a small programme in C++ that can covert from the old currency into the new one. I would like this programme to run behind a simple...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
1
by: chean | last post by:
Write a program that creates a simple calculator . The user enters two numbers in the text fields, Number 1 and Number 2. There are 4 buttons, labeled with “Add”, “Substract”, “Multiply” and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.