Current location: Hot Scripts Forums » Programming Languages » PHP » php date $var, ignores offset at midnight ?!?


php date $var, ignores offset at midnight ?!?

Reply
  #1 (permalink)  
Old 04-13-05, 02:16 AM
cemm cemm is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Question php date $var, ignores offset at midnight ?!?

Hi all,

I really hope that some of you can help me understand the following problem, and hopefully offer a solution to the problem...

Background: I'm writing a script, that uses both local server time and also uses US/Pazific time. The script uses $_sessions and a automatic page refresh every 3 minuttes. I want users of the script to be able to define $offset in the config file, that holds the time difference in hours from their local time to US/Pazific time. $ldate + $ltime represents local server time, and $udate + $utime represents US/Pazific time.

The code:

PHP Code:

$offset"-9";


$ldate date("Y-m-d"time());
$ltime date("H:i:s"time());

$udate_tmp mktime (date("h")+$offset,date("i"),date("s"),date("m"),date("d"),date("y"));
$udate date("Y-m-d"$udate_tmp);
$utime date("H:i:s"$udate_tmp); 
The problem:

When the server the script runs on passes midnight, php completelly ignores $offset and $udate_tmp. Which results in $udate,$utime is equal to $ldate and $ltime. In my case, my server is 9 hours ahead of US/Pazific time. However, I want to be able to offer the script to other users, and they must also be able to define $offset, and thereby showing both their local time and US/Pazific time.

Can anyone offer me insight to the problem here ?

Is it caused by the Apache server ?

Should the code be done in an other way?

All help is appreciated
Reply With Quote
  #2 (permalink)  
Old 04-13-05, 07:50 PM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
based on your problem, i can find wrong logic here. if you decremented the time with the offset, why did you only change the hour? when the result is negative, it means your in another day and not in the same day. i think you can see this from your code.

some workaround:
1. you can set an absolute reference to GMT (gmdate and gmmktime)
2. you should do proper maths to calculate relative time difference from your server time.

regards,
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #3 (permalink)  
Old 04-14-05, 02:21 AM
cemm cemm is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
I actually think I found the bug yesterday.

The date("h")+$offset, should naturally be in 24 hours format, when using it with 24h format later on.

So I changed it to : date("H")+$offset, and it works like a charm.

Thank you for your reply Moronovich :-)
Reply With Quote
  #4 (permalink)  
Old 04-14-05, 05:34 PM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
you solve the problem yourself. anyway, for me, it's working but it's not working properly. let's assume date('H') is equal to i which 0<i<9, i E A. if we supply date('H') with, let's say 5, now the hour would be: 5-9 = -4. this is quite impossible for your script to return the proper time. instead, you should do proper math to solve this issue.
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #5 (permalink)  
Old 04-16-05, 06:21 AM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Yes, you should stick to the timestamps...
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #6 (permalink)  
Old 04-16-05, 02:42 PM
JBChris JBChris is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
I've come up with a script that displays this right... I'm waiting for my USB flash drive to come back from replacement... I have to get it off the computer here that doesn't have internet... I'll post it here when I get it. So far it works with any given time and time offset.
Reply With Quote
  #7 (permalink)  
Old 04-19-05, 07:04 PM
JBChris JBChris is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Okay, here you are.

It works for any timezone that I've tried it on, including positive GMT offsets. This code prints the formatted date in central time no matter where the server is. The timezone can be changed by simply changing "$gmt" to your offset. If it needs clarification, or modification to suit your purposes, let me know.
PHP Code:

<?php

//get GMT offset at the server
$offset date('O');
//algebraically add the GMT offset for central time (600)
$gmt "-600";
$time_off $offset $gmt;
//divide it by 100 to get hours
$off $time_off/100;
//if time is in DST, add an hour to the offset
$offset $off date('I');
//make the timestamp
$maketime mktime(date('G') - $offsetdate('i'), date('s'), date('n'), date('j'), date('Y'), date('I'));
//print the formatted date using the timestamp
print date('l, F jS, Y - g:i a'$maketime);
?>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Future Date Calculate in PHP websmart Script Requests 4 01-16-05 09:56 PM
PHP Function to find if a variable is a valid date? jozin PHP 4 11-07-04 02:03 AM
Porting from PHP to C++ whizmail C/C++ 1 10-25-04 12:52 PM
striping date in php shaheda PHP 1 06-08-04 09:52 AM
converting php date picker to asp AshleyQuick ASP 3 05-24-04 11:52 PM


All times are GMT -5. The time now is 06:39 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.