Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Daylight Savings Time and Timestamps 1

Status
Not open for further replies.

cutley

Programmer
Jan 8, 2003
17
US
I have a PHP app where I have to schedule people for X number of weeks at a time. I simply take my UNIX timestamp and increment it by 604800 for each week I need to schedule, which works great with one exception...

The TIME in the timestamp changes when I hit April for Daylight Savings time. So if somebody works 8-4 every Monday, suddenly that becomes 9-5 according to the timestamps I'm looking at.

So now instead of simply being able to increment a timestamp by 604800 each time through a FOR loop, I have to do something else. I just don't know what that something else is, it has got to be easy, right?

Any suggestions would be appreciated! Thanks in advance...
 
PHP has built in date and time functions which will allow you to just add 7 days, rather than 604800 seconds... and these functions have built in support for daylight savings time.


If that leaves you with any questions let me know and hopefuly I can expand on the manual... (take note of the left column which has related functions you may find useful).

-Rob
 
Thanks for the info, but I have been over the fucntion reference already to no avail. I went back and looked again hoping to find something I may have missed.

I'm working with UNIX timestamps and MKTIME doesn't seem to take timestamps as a parameter which would require me to do some converting back and forth in a script that is already doing TONS of work.

I cannot find a function that would allow me to increment a UNIX timestamp by one-week while taking into account that I never want to apply Daylight savings time. That's what it really boils down to, if I'm scheduling somebody today for 9-5 on MWF, then I want it to be 9-5 MWF in April too - despite Daylight savings time.

Is there something I'm missing? Is there a fucntion to do this, or must I code in a bunch of logic to determine whether or not I'm creating a schedule entry during daylight savings time, and if so subtract an hour from the start and stop times.
 
Have you looked into date? It does take unix timestamps...


Combining that with mktime works really well... here's an example...

$nextweek = mktime(0,0,0,date("m"), date("d")+7, date("Y"));

or for your example

$st = whateveryourtimestampis;

$nextweek = mktime(0,0,0,date("m", $st), date("d", $st)+7, date("Y", $st));

Hope this helps....
( does warn that the daylight savings time issue is not 100%, but that's an old note, and I have no idea how well it works, perhaps some others can enlighten us)


-Rob
 
Rob, the mktime/date example is handy in terms of adding dates, but it still makes the adjustment for daylight savings time?

Perhaps the problem is not the fucntions that I use to calculate the dates, but the function I use to display them? I need to look into that... In my loop I print the dates right now more or less to debug things, so I can figure out what works before putting stuff in my database.

Here's the FOR loop I've been using.

for ( $i=52; $i > 0; $i-- ) {

# Display for debug purposes, this would be a DB
# insert in the working application.
$nextMonStartDateTime = date( "r", $nextMondayStart );
$nextMonStopDateTime = date( "r", $nextMondayStop );
echo &quot;$nextMonStartDateTime $nextMonStopDateTime - $nextMondayStart<br>&quot;;

# Then at the bottom of my FOR loop I increment by one-week
$nextMondayStart = $nextMondayStart + $oneweek;
$nextMondayStop = $nextMondayStop + $oneweek;

}

So once this loop hits April 4, it starts printing my start and stop times with the extra hour tacked on - so a 9 to 5 schedule would become 10 to 6, etc..

Even if I use mktime and date to increment the weeks, I still don't see how I can avoid getting the scheduled messed up during daylight savings time.

I feel like a dunce here, sorry! This date/time stuff is giving me fits.
 
Good point, regardless of daylight savings the seconds from the Unix Epoch are going to the be the same aren't they?

So yes, it would be in the display... but I'd imagine just about anything that converts from a timestamp to a date should know how to handle that...

Now I'm muddling with you, but maybe two heads'll prevail... with &quot;r&quot; you're getting something like..

Thu, 21 Jan 2002 16:01:07 +0200

and after April 4th the hours are wrong... but does the +0200 switch to +0100 by any chance?

Try changing your date calls to
date(&quot;I - r&quot;, ...)

and see what happens to the value of I, it's the flag for DST.

...
I'm trying,
Rob

 
Ok, so I finally went ahead and just put your code on my machine, and that's exactly what's happening... PHP knows the time is DST as long as the server has it's times set right.... so now to boggle through that one.

-Rob
 
Shoot, that didn't quite get it either. I'm pretty sure I could do this by adding a condition to check each date to see if it is within the range of daylight savings time, but that seems like a nasty kludge that will slow my page down.

Anyhow, this is what the output looks like after changing date to use &quot;l - r&quot;:

0 - Mon, 24 Mar 2003 06:00:00 -0700 0 - Mon, 24 Mar 2003 08:00:00 -0700 - 1048510800
0 - Mon, 31 Mar 2003 06:00:00 -0700 0 - Mon, 31 Mar 2003 08:00:00 -0700 - 1049115600
1 - Mon, 7 Apr 2003 07:00:00 -0600 1 - Mon, 7 Apr 2003 09:00:00 -0600 - 1049720400

Here's a few lines before I made the change:

Mon, 24 Mar 2003 06:00:00 -0700 Mon, 24 Mar 2003 08:00:00 -0700 - 1048510800
Mon, 31 Mar 2003 06:00:00 -0700 Mon, 31 Mar 2003 08:00:00 -0700 - 1049115600
Mon, 7 Apr 2003 07:00:00 -0600 Mon, 7 Apr 2003 09:00:00 -0600 - 1049720400
Mon, 14 Apr 2003 07:00:00 -0600 Mon, 14 Apr 2003 09:00:00 -0600 - 1050325200
 
Please excuse the timestamp on the end of that exmaple output. I mention it only because it may confuse matters. I changed the echo in my original code example to:

echo &quot;$nextMonStartDateTime $nextMonStopDateTime - $nextMondayStart<br>&quot;;

Thanks!
 
Ok, I think our minds are approaching this wrong... PHP is doing everything right.

1 week from Monday 9 am is Monday 8 am after Daylight savings time...


so... here's my workaround for what you want (using just one time)...

extract the hour your referring to, and then supply that to the maketime function...

$st is your initial timestamp, and $days_offset = 7*$i in your for loop.

<?

$st = time();

for ( $i=52; $i > 0; $i-- ) {
$days_offset=7*$i;
# Display for debug purposes, this would be a DB
# insert in the working application.

$nextweek = date(&quot;r&quot;, mktime(date(&quot;h&quot;, $st),0,0,date(&quot;m&quot;, $st), date(&quot;d&quot;, $st)+$days_offset, date(&quot;Y&quot;, $st)));

echo &quot;$nextweek<BR>&quot;;

# Then at the bottom of my FOR loop I increment by one-week


}
?>


 
Sorry, I didn't clean up the comments approprately... but that does work.

-Rob
 
oh shoot, I'm assuming people can start at anytime they want, so you really should have

date(&quot;r&quot;, mktime(date(&quot;h&quot;, $st), date(&quot;i&quot;, $st), date(&quot;s&quot;, $st), date(&quot;m&quot;, $st), date(&quot;d&quot;, $st)+$days_offset, date(&quot;Y&quot;, $st)));

-Rob
 
Ok, my apologies for so many posts... in the interests of the teaching forum that Tek-Tips is, I want to explain what I did just in case it wasn't clear... (I had a slight emergency here so I just posted the code at the time)

so, mktime creates the unix timestamp from a given (hour,minute,second,month,day,year)

date then converts all of this information into whatever format you so choose.

So what I did was take the original timestamp from the database, $st, and extract each field from it, hours, minutes etc...

then I provided those fields with a variable day to the date function.

So PHP is actually calculating you a new Unix Timestamp for 09:00:00 Apr, 4th, 2003, rather than the other approach of figuring out what time it is a week from now.

Hope that's clear... end of the day!

-Rob
 
That solution is clever and I understand it (the best kind). I think it's also faster than what I was trying to do before, so it's a great solution for my problem and is something I will probably use many times in the future. Thanks for working through it with me. I think I could do better in terms of forming my questions, I'll work on that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top