473,386 Members | 1,706 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,386 software developers and data experts.

PHP Time Bug

I have a very interesting problem...
Php says that the difference between these times is 1 hour.

<?php
$t1 = "2005-12-31 20:00";
$t2 = "2005-12-31 20:00";
$t1i = strtotime($t1);
$t2i = strtotime($t2);
$diff = abs($t1i - $t2i);
print_r(getdate($diff));
?>

The Output:
Array
(
[seconds] => 0
[minutes] => 0
[hours] => 1
[mday] => 1
[wday] => 4
[mon] => 1
[year] => 1970
[yday] => 0
[weekday] => Thursday
[month] => January
[0] => 0
)

Can you please suggest something how to calculate the difference
between 2 datetimes?

And another question, a bit off-topic, but how do I get the number of
days of a month with taking leap years in consideration?

Thanks in advence.
Tomas

Feb 2 '06 #1
3 1480

"Tomas" <to********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I have a very interesting problem...
Php says that the difference between these times is 1 hour.

<?php
$t1 = "2005-12-31 20:00";
$t2 = "2005-12-31 20:00";
$t1i = strtotime($t1);
$t2i = strtotime($t2);
$diff = abs($t1i - $t2i);
print_r(getdate($diff));
?>

The Output:
Array
(
[seconds] => 0
[minutes] => 0
[hours] => 1
[mday] => 1
[wday] => 4
[mon] => 1
[year] => 1970
[yday] => 0
[weekday] => Thursday
[month] => January
[0] => 0
)

Can you please suggest something how to calculate the difference
between 2 datetimes?

And another question, a bit off-topic, but how do I get the number of
days of a month with taking leap years in consideration?

Thanks in advence.
Tomas


function is_leap_year($year) {
return (($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0);
}

function days_in_month($month, $year) {

switch ($month) {
case 4:
case 6:
case 9:
case 11:
return 30;

case 2:
return is_leap_year($year) ? 29 : 28;

default:
return 31;
}
}
Marc.


Feb 2 '06 #2
On 2 Feb 2006 14:01:54 -0800, "Tomas" <to********@gmail.com> wrote:
I have a very interesting problem...
Php says that the difference between these times is 1 hour.

<?php
$t1 = "2005-12-31 20:00";
$t2 = "2005-12-31 20:00";
$t1i = strtotime($t1);
$t2i = strtotime($t2);
$diff = abs($t1i - $t2i);
print_r(getdate($diff));
?>

The Output:
Array
(
[seconds] => 0
[minutes] => 0
[hours] => 1
[mday] => 1
[wday] => 4
[mon] => 1
[year] => 1970
[yday] => 0
[weekday] => Thursday
[month] => January
[0] => 0
)
What you've ended up asking with the code above is "what is the year, month,
day and time of the difference of two identical times?" - which doesn't make
much sense.

You end up with "what is the date represented by the value zero", which is
defined as the "UNIX epoch". Apparently you're in a central European country
currently on GMT+1 (Slovakia from the looks of your message headers?); the UNIX
epoch is midnight 1st Jan 1970 GMT - the result you've got is 1am on the same
day, as adjusted for your timezone.
Can you please suggest something how to calculate the difference
between 2 datetimes?
Depends what you really want. You already had the difference in seconds -
zero. You can divide this down to get hours or days, but it depends how you
want to deal with summer time transitions.
And another question, a bit off-topic, but how do I get the number of
days of a month with taking leap years in consideration?


The "t" option in http://uk2.php.net/date
Or http://uk2.php.net/calendar

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Feb 2 '06 #3
Tomas wrote:
I have a very interesting problem...
Php says that the difference between these times is 1 hour.
No it doesn't
<?php
$t1 = "2005-12-31 20:00";
$t2 = "2005-12-31 20:00";
$t1i = strtotime($t1);
$t2i = strtotime($t2);
$diff = abs($t1i - $t2i);
print_r(getdate($diff));
?>
What PHP says is that getdate(0) returns the following
The Output:
Array
(
[seconds] => 0
[minutes] => 0
[hours] => 1
[mday] => 1
[wday] => 4
[mon] => 1
[year] => 1970
[yday] => 0
[weekday] => Thursday
[month] => January
[0] => 0
)

Can you please suggest something how to calculate the difference
between 2 datetimes?
You already did that.
The difference between $t1 and $t2 is 0 (seconds); if you want to
convert seconds to hours divide by 3600

echo 'Difference is ', $diff / 3600, 'hours';
And another question, a bit off-topic, but how do I get the number of
days of a month with taking leap years in consideration?


$month = 2;
$year = 2000;
$number_of_days2000 = date('t', gmmktime(12, 0, 0, $month, 15, $year));
$year = 2004;
$number_of_days2004 = date('t', gmmktime(12, 0, 0, $month, 15, $year));

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Feb 2 '06 #4

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

Similar topics

8
by: Bart Nessux | last post by:
am I doing this wrong: print (time.time() / 60) / 60 #time.time has been running for many hours if time.time() was (21600/60) then that would equal 360/60 which would be 6, but I'm not getting...
5
by: David Stockwell | last post by:
I'm sure this has been asked before, but I wasn't able to find it. First off I know u can't change a tuple but if I wanted to increment a time tuple by one day what is the standard method to do...
6
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript camp - bit confused about that. Anyway, can anyone...
3
by: Szabolcs Nagy | last post by:
I have to measure the time of a while loop, but with time.clock i always get 0.0s, although python manual sais: "this is the function to use for benchmarking Python or timing algorithms" So i...
6
by: Rebecca Smith | last post by:
Today’s question involves two time text boxes each set to a different time zone. Initially txtCurrentTime will be set to Pacific Time or system time. This will change with system time as we travel...
3
by: luscus | last post by:
Thanks for all the responses on my first question. Unfortunately the answers I was given were too complicated for my small brain , and neophite condition to understand. So if you could talk down to...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
1
by: davelist | last post by:
I'm guessing there is an easy way to do this but I keep going around in circles in the documentation. I have a time stamp that looks like this (corresponding to UTC time): start_time =...
2
by: Roseanne | last post by:
We are experiencing very slow response time in our web app. We run IIS 6 - windows 2003. I ran iisstate. Here's what I got. Any ideas?? Opened log file 'F:\iisstate\output\IISState-812.log'...
9
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.