LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
LinkBack Search this Thread
Old 06-04-2010, 12:50 PM   #1
tdnnash25
Member
 
Registered: Apr 2009
Posts: 63

Rep: Reputation: 15
Question help with some php code


Sorry if this is not the appropriate forum. Experienced PHP developers, please advise:


My server is in Kansas (Central time). Right now, due to DST, we are
GMT -5...in the Winter GMT -6

my form gives users the option to select a date, time, and timezone
for a specific event to occur. So, say the user is in California and
they choose 8:00AM, January 1st 2011, Pacific Time (2 hours behind
us).

Well, I want my script to take the date, time, and timezone they
choose and convert it to central time so I can timestamp it on my
server ... so for this example, I'd timestamp the file 10:00AM,
January 1st 2011 ... then on that date, the event would occur at
10:00AM my time, 8:00AM their time.

I've got some code that is getting close to accomplishing this....do
you have any experience with timezone stuff?

my code:

$userTimezone = new DateTimeZone($tz);
$myDateTime = new DateTime($time);
$cstTimeZone = new DateTimeZone('CST');
$myDateTime = new DateTime($time, $userTimezone);

echo $myDateTime->format('r'); // browser prints this: Sat, 01 Jan
2011 08:00:00 -0800
$offset = $cstTimeZone->getOffset($myDateTime);
echo "<br />";
echo $offset; // browser prints this: -21600
echo "<br />";
echo date('Y-m-d H:i:s', $myDateTime->format('U') + $offset); //
browser prints this: 2011-01-01 04:00:00
echo "<br />";

so, the first echo is correct...it prints the date and time the user
chose ... plus the GMT time

I'm going off of an article I found here(http://blog.boxedice.com/
2009/03/21/handling-timezone-conversion-with-php-datetime/), but I
must be doing something wrong b/c the last echo certainly doesn't
print out the correct time I'm looking for.
 
Old 06-04-2010, 03:30 PM   #2
eSelix
Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Kubuntu
Posts: 809

Rep: Reputation: 162Reputation: 162
Check this:
PHP Code:
$aMyTimeZone "CST";
$aUserTimeZone "US/Pacific";
$aUserTime "2011-01-01 08:00";

$MyTimeZone = new DateTimeZone($aMyTimeZone);
$UserTimeZone = new DateTimeZone($aUserTimeZone);
$UserTime = new DateTime($aUserTime$UserTimeZone);

$MyTime = new DateTime('@'.(string)$UserTime->format('U'));
$MyTime->setTimeZone($MyTimeZone);

echo 
'USER:<br>';
echo 
$UserTime->format('r').'<br><br>';
echo 
'MY:<br>';
echo 
$MyTime->format('r').'<br>'
If you have PHP >= 5.3 you can use DateTime::getTimestamp() and DateTime::createFromFormat().
Also I don't known why
PHP Code:
$MyTime = new DateTime('@'.(string)$UserTime->format('U'), $MyTimeZone); 
does not set timezone, but compute it.
 
Old 06-04-2010, 03:41 PM   #3
tdnnash25
Member
 
Registered: Apr 2009
Posts: 63

Original Poster
Rep: Reputation: 15
Smile thank you!!

Quote:
Originally Posted by eSelix View Post
Check this:
PHP Code:
$aMyTimeZone "CST";
$aUserTimeZone "US/Pacific";
$aUserTime "2011-01-01 08:00";

$MyTimeZone = new DateTimeZone($aMyTimeZone);
$UserTimeZone = new DateTimeZone($aUserTimeZone);
$UserTime = new DateTime($aUserTime$UserTimeZone);

$MyTime = new DateTime('@'.(string)$UserTime->format('U'));
$MyTime->setTimeZone($MyTimeZone);

echo 
'USER:<br>';
echo 
$UserTime->format('r').'<br><br>';
echo 
'MY:<br>';
echo 
$MyTime->format('r').'<br>'
If you have PHP >= 5.3 you can use DateTime::getTimestamp() and DateTime::createFromFormat().
Also I don't known why
PHP Code:
$MyTime = new DateTime('@'.(string)$UserTime->format('U'), $MyTimeZone); 
does not set timezone, but compute it.
thank you so much ... I think it is working as expected, I will test it out for a while and post back here if I have any other questions. I *really* appreciate your feedback
 
Old 06-04-2010, 03:54 PM   #4
tdnnash25
Member
 
Registered: Apr 2009
Posts: 63

Original Poster
Rep: Reputation: 15
How would I get this:
MY:
Thu, 24 Jun 2010 14:20:00 -0500

to look like this:
My:
20100624142000

?
 
Old 06-04-2010, 04:05 PM   #5
tdnnash25
Member
 
Registered: Apr 2009
Posts: 63

Original Poster
Rep: Reputation: 15
also, I'm not familiar with this: '@'.(string)

what does it mean?
 
Old 06-04-2010, 07:00 PM   #6
eSelix
Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Kubuntu
Posts: 809

Rep: Reputation: 162Reputation: 162
Look at the possible formats of date and time.
Ex. echo $MyTime->format('YmdHis');

About '@' look at http://pl2.php.net/manual/en/datetim...s.compound.php
It tells DateTime constructor that its parameter is an Unix Timestamp.

The '(string)' convert variable to string type, for safety to concatenation. It is not required here, it's just my style of programming.
 
  


Reply

Tags
timezone


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Yesterday PHP worked just fine and today it wants to serve up the php code. orsty9001 Linux - Server 10 12-12-2008 10:48 AM
php page displaying text that is supposed to be part of php code DragonM15 Programming 9 07-31-2008 04:58 PM
Postfixadmin PHP setup file - only displays PHP code davidmbecker Linux - Software 3 04-17-2008 10:33 AM
executing C++ code from within PHP code vineet7kumar Programming 1 06-03-2007 04:13 PM
merge ASP code with PHP code.. possible ?? ALInux Programming 7 12-30-2005 08:40 AM


All times are GMT -5. The time now is 05:47 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration