Question:
PHP gmdate with timezone offset - date works need help with time?
2011-05-17 11:02:31 UTC
Ok my timezone number would be -8, so I have that stored in a variable of $timezone. Then I have two variables that use this to get the gmdate DATE and TIME in $date and $time variables. Here is the code:

$timezone = '-8';
$date = gmdate ("m-d-Y" , time() + 3600 * ( $timezone + gmdate(' I ')));
$time = gmdate ("h:i:s A" , time() + 3600 * ( $timezone + gmdate( ' I ')));

Note - I added spaces so Yahoo doesn't cut the code off.

Now here is what I need help with. The $date variable always comes out right, it gives me the proper date, or day I should say, when used. $time however dose not work like it should. It returns a time that is 7 hours ahead of my timezone set at -8.

What am I missing? How can I get the time to come out right? - YES my timezone number is really -8, thats not wrong, and the final part of the code with gmdate(' I ') takes into account day light savings time so even if that part was messed up my time would be off by 1 hour not 7.
Three answers:
2011-05-17 16:03:18 UTC
This isn't exactly answering your question, but I know another method that may help you out. It requires php 5.



Try something like the following:



date_default_timezone_set( 'America/Los_Angeles' ); //This is the timezone

$date = date( 'm-d-Y', time());

$time = date( 'h:i:s A', time());



I've used it before and it worked great. Daylight savings isn't even an issue!



------------------------------------------



Being precise in your questions is nice, instead of giving a negative response to an answer that is correct. Anyway...



Do you know full well you can use putenv( "TZ=America/Los_Angeles" ); instead?



It's easier than calculating if the current date is in daylight savings for the timezone you are interested in. putenv() will only exist for the duration of the script.



Finally, in regards to your code, gmdate( 'I' ) will always return zero because Greenwich Mean Time doesn't have daylight savings. Secondly, your code running as-is results in a time exactly one hour off on my own machine and another server (running php 4).
ursua
2016-12-31 19:05:11 UTC
Php Timezone Offset
?
2016-09-29 09:17:26 UTC
Php Gmdate


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...