473,789 Members | 2,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

month duration problem

Hi!

I'm writing a script that would sort some events and calculate the sum
of them all in the end of the month, which should be equal to the
number of days in a month. It seems to be working well, but for some
reason on march it calculates one hour less (30 days and 23:00:00) and
in october one hour more (31 days 1:00:00). I've tried different years
not just 2007. It's all the same. Anyone knows a good reason why this
might happen as it works like a charm for other months. Please help me
solve this, as I really dont like the solution to add another hour to
March and subtract one from October...

Here are some relavant parts of the code (I've translated it, so there
might be some old variable namse in there):

function duration($Time1 , $Time2)
{

$diff = $Time2 - $Time1;

$SecondsInDay = 86400;
$SecondsInHour = 3600;
$SecondsInMinut e = 60;

$days = floor($diff /$SecondsInDay );
$hours = floor(($diff - $days *$SecondsInDay )/$SecondsInHour );
$mins = floor(($diff - $days *$SecondsInDay - $hours *$SecondsInHour )/
$SecondsInMinut e );
$secs = floor($diff - $days *$SecondsInDay - $hours *$SecondsInHour -
$mins *$SecondsInMinu te );
$result = "$days days $hours:$mins:$s ecs";

return $result;

}
$Year= $_GET['year'];
$Month= $_GET['month'];

$FirstTime = mktime(0,0,0,$M onth,1,$Year);
$LastTime = mktime(0,0,0,($ Month+1),1,$Yea r);

$FirstDate = date("d.m.Y H:i:s", $FirstTime);
$LastDate = date("d.m.Y H:i:s", $LastTime);

echo "$FirstDate - $LastDate";
//these shows like it should for all months:
//Jan: 01.01.2007 00:00:00 - 01.02.2007 00:00:00
//Feb: 01.02.2007 00:00:00 - 01.03.2007 00:00:00
//March: 01.03.2007 00:00:00 - 01.04.2007 00:00:00
//April: 01.04.2007 00:00:00 - 01.05.2007 00:00:00
//May: 01.05.2007 00:00:00 - 01.06.2007 00:00:00
//...
//October: 01.10.2007 00:00:00 - 01.11.2007 00:00:00

$MonthDurationI nSeconds = $LastTime - $FirstTime;
$MonthDuration = duration($First Time, $LastTime);

echo "$MonthDuration InSeconds; $MonthDuration" ;
//for two months of same length this above is different and I cant
figure out why...
//Jan: 2678400, 31 days 00:00:00
//Feb: 2419200, 28 days 00:00:00
//March: 2674800, 30 days 23:00:00
//April: 2592000, 30 days 00:00:00
//May: 2678400, 31 days 00:00:00
//...
//October: 2682000, 31 days 01:00:00
Jul 1 '08 #1
5 1645
La***********@g mail.com wrote:
[...] It seems to be working well, but for some reason on march it
calculates one hour less (30 days and 23:00:00) and in october one hour
more (31 days 1:00:00).
Why not use something like this?
$days_in_month =
date( 't' , mktime(0,0,0,$M onth,1,$Year) );

$MonthDurationI nSeconds = $days_in_month * 24 * 3600;


Cheers,
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
Jul 1 '08 #2
Thanks :) It worked.

Regards,
Lado
Jul 1 '08 #3
La***********@g mail.com wrote:
Hi!

I'm writing a script that would sort some events and calculate the sum
of them all in the end of the month, which should be equal to the
number of days in a month. It seems to be working well, but for some
reason on march it calculates one hour less (30 days and 23:00:00) and
in october one hour more (31 days 1:00:00). I've tried different years
not just 2007. It's all the same. Anyone knows a good reason why this
might happen as it works like a charm for other months. Please help me
solve this, as I really dont like the solution to add another hour to
March and subtract one from October...

Here are some relavant parts of the code (I've translated it, so there
might be some old variable namse in there):

function duration($Time1 , $Time2)
{

$diff = $Time2 - $Time1;

$SecondsInDay = 86400;
$SecondsInHour = 3600;
$SecondsInMinut e = 60;

$days = floor($diff /$SecondsInDay );
$hours = floor(($diff - $days *$SecondsInDay )/$SecondsInHour );
$mins = floor(($diff - $days *$SecondsInDay - $hours *$SecondsInHour )/
$SecondsInMinut e );
$secs = floor($diff - $days *$SecondsInDay - $hours *$SecondsInHour -
$mins *$SecondsInMinu te );
$result = "$days days $hours:$mins:$s ecs";

return $result;

}
$Year= $_GET['year'];
$Month= $_GET['month'];

$FirstTime = mktime(0,0,0,$M onth,1,$Year);
$LastTime = mktime(0,0,0,($ Month+1),1,$Yea r);

$FirstDate = date("d.m.Y H:i:s", $FirstTime);
$LastDate = date("d.m.Y H:i:s", $LastTime);

echo "$FirstDate - $LastDate";
//these shows like it should for all months:
//Jan: 01.01.2007 00:00:00 - 01.02.2007 00:00:00
//Feb: 01.02.2007 00:00:00 - 01.03.2007 00:00:00
//March: 01.03.2007 00:00:00 - 01.04.2007 00:00:00
//April: 01.04.2007 00:00:00 - 01.05.2007 00:00:00
//May: 01.05.2007 00:00:00 - 01.06.2007 00:00:00
//...
//October: 01.10.2007 00:00:00 - 01.11.2007 00:00:00

$MonthDurationI nSeconds = $LastTime - $FirstTime;
$MonthDuration = duration($First Time, $LastTime);

echo "$MonthDuration InSeconds; $MonthDuration" ;
//for two months of same length this above is different and I cant
figure out why...
//Jan: 2678400, 31 days 00:00:00
//Feb: 2419200, 28 days 00:00:00
//March: 2674800, 30 days 23:00:00
//April: 2592000, 30 days 00:00:00
//May: 2678400, 31 days 00:00:00
//...
//October: 2682000, 31 days 01:00:00
But March is one hour short, and October one hour long due to daylight
savings time.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jul 1 '08 #4
NC
On Jul 1, 12:37 am, Lado.Lesko...@g mail.com wrote:
>
I'm writing a script that would sort some events and calculate the sum
of them all in the end of the month, which should be equal to the
number of days in a month. It seems to be working well, but for some
reason on march it calculates one hour less (30 days and 23:00:00) and
in october one hour more (31 days 1:00:00).
Which is correct, because of daylight saving time. In March, clocks
are
moved one hour ahead, so you lose an hour. In October, clocks are set
one
hour back, so you gain an hour.
Please help me solve this,
You might recall that date('t', $timestamp) returns the number of
days
in the month into which the $timestamp falls. You can then use that
number to compute the month's duration in seconds.

Cheers,
NC
Jul 2 '08 #5
I had to do some changes to my script to include longer-than-month
periods, so I had to use the first solution again. This brought up the
same problem again, but after thinking about what you guys said, I
realised it's actually better this way.

Thanks!
Jul 8 '08 #6

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

Similar topics

5
1866
by: Dave | last post by:
I am working with a proprietary database that records the date, time, location, and speed of a vehicle. It is pulling this information from GPS unit tied to a vehicle. The table is populated with values that are pulled from the GPS unit every 30 seconds. I need to find the duration of time for when a vehicle is stopped. I have created a cursor that runs though all of the tables, and gathers the data for when the vehicle's speed is equal to...
1
2245
by: Dave | last post by:
Greetings, I am trying to create a duration field in a query. I have a field with a start time and a field with an end time. They are both in military time, and are formatted as hours:minutes. I am trying to create a duration field by subtracting the start time from the end time. This is working. However, some end times do not happen until the following day, and this is screwing up the results. If the start time is 04:30 and the
6
77857
by: Ashish Sheth | last post by:
Hi All, In C#, How can I get the difference between two dates in number of months? I tried to use the Substract method of the DateTime class and it is giving me the difference in TimeSpan,From which I can get the duration in days, hours and so.. but how can I get the difference in months? Please reply ASAP. it's urgent. -- regards, Ashish Sheth
7
1856
by: Novice Computer User | last post by:
Hi. Can somebody PLEASE help. I have spent hours on this.. but I am a total novice and can't seem to figure it out. Here is a .php script. Right now, the minimum amount of time (i.e. duration) allowed is 1 month. However, I want to be able to reduce the minimum amount of time to 1 day (i.e. 24 hours) -- basically a 24 hour trial period. Can somebody PLEASE PLEASE tell me what I need to change in this file to do this? Here is a...
22
31212
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for June, and will return all records in that month.
1
2799
mangat
by: mangat | last post by:
Hi guy's i'm stuck in a similar problem. The problem is that i have two fields one shows the LogIn time of the user and other shows the Duration since the user LogIn and now i want to store the logout time which is addition of LogIn time and Duration How i achive this.Plz Help me. thanx In advance. Mangat Phogat
15
2055
by: bcpkh | last post by:
Hello All I have a simple task that is driving me crazy. A string representing a duration in the following format is passed to my application, a function is dedicated to convert this duration to seconds; H:MM:SS e.g. 0:00:00 or 00:12:45
5
19824
by: Mike | last post by:
I use c#, V2005 How I can get difference between two dates and get value in month(s) I had found some solutions but it is not exactly what I need. private static int monthDifference(DateTime startDate, DateTime endDate) { int monthsApart = 12 * (startDate.Year - endDate.Year) +
1
3761
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: - "function prototype scope" for structures and unions? - "extern" for internal linkage ?
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.