2

I am trying to have a line of code added to an html document that is preceded by the time. I want the time zone to be relative to me, however I cannot change it from the default UTC. I have changed in the php.ini file to PST as well as using date_default_timezone_set('America/Los_Angeles'); and yet it still prints the time 7 hours ahead of my timezone. Heres the code that deals with the time:

session_start();
if(isset($_SESSION['name']))
{
    date_default_timezone_set('America/Los_Angeles');

    $msg = $_POST['text'];

    $fo = fopen("log.html", 'a');
    fwrite($fo, "<div class=msgln>(".date("g:i A").") <b  style=color:red;>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($msg))."<br></div>
    ");
    fclose($fo);
}
Xander Luciano
  • 3,753
  • 7
  • 32
  • 53
  • Lost FTP connection and the file failed to update. I didn't see this and was wondering why it didn't make any difference. – Xander Luciano Aug 11 '12 at 04:38
  • At which point did this not work? Just curious so we know what fixed your immediate problem. – BrianS Aug 11 '12 at 04:42
  • Heres basically what happened, I was editing the code on my local computer and saving is supposed to just auto-uploade/overwrite the preexisting file, however my connection was lost(wifi dropped dead for a second or something) and when i saved instead of connecting, it just saved the file to my hard drive. I just reconnected and uploaded the file and the problem was solved. – Xander Luciano Aug 11 '12 at 04:45

3 Answers3

5

Servers should be set to UTC, and you should not be looking to change the default. Instead, what you want to do is create a DateTime object based on the time, then convert it to the timezone you want, and display.

$now = new DateTime();
$now->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $now->format('g:i A');

I don't know if your format string is valid or not, but the format method is suppossed to be compatible with that accepted by the date() function you were using in your original example.

gview
  • 14,876
  • 3
  • 46
  • 51
  • Even though this didn't answer the question it is the proper way to do this so in case anyone else comes across this post i will mark yours as correct. – Xander Luciano Aug 11 '12 at 04:38
  • 1
    I don't know if I'd say *should*, but using UTC does make some time-based tasks a bit easier. Then again, all you really need is an accurately-recorded time which can then be easily translated if necessary. I totally agree, however, that what you don't want to do is regularly change the server time/timezone as part of script execution. – BrianS Aug 11 '12 at 04:51
  • Although if we're talking RFC language I guess *should* is appropriate ;) ... good discussion on using and saving time here: http://stackoverflow.com/q/2532729/264628 – BrianS Aug 11 '12 at 05:10
1

First make sure you're using a valued timezone. You can find a list of supported timezones in the PHP docs.

The second problem is using date() without specifying the timestamp. This defaults to the timestamp produced by time() which (based on a comment in the documentation) is UTC time. You'll either have to use strftime() or manually subtract the difference from UTC.

BrianS
  • 13,284
  • 15
  • 62
  • 125
  • Oh right, i also tried America/Los_angeles and that didn't work as well – Xander Luciano Aug 11 '12 at 04:17
  • Capitalization issue? The list says *America/Los_Angeles* – BrianS Aug 11 '12 at 04:18
  • Copy and pasted straight off the site so wasn't that. Everywhere i've read it says this is how you are supposed to do it, but its not working? – Xander Luciano Aug 11 '12 at 04:20
  • Don't know if this comment is still relevant: http://us.php.net/manual/en/function.time.php#100220 – BrianS Aug 11 '12 at 04:23
  • Looking into it I came across this: http://php.net/manual/en/function.strftime.php and it appears that this function has some issues with cross platform. What would be the best way to just subtract 7 hours from the hour? – Xander Luciano Aug 11 '12 at 04:29
  • Unless you're designing for cross-platform support (or developing on different platforms) I wouldn't worry about that. – BrianS Aug 11 '12 at 04:30
  • However, you can calculate a 7-hour difference. `time()` returns the time in seconds, so `time()-(7*60)` – BrianS Aug 11 '12 at 04:32
  • I just found out the source of my error. my ftp connection was lost and everytime i went to save it just saved the temp file and not actually update on my server. I re-established connection and uploaded the file and it worked just fine. – Xander Luciano Aug 11 '12 at 04:37
1

If you use 'etc/GMT' you can set the dateTime object to the desired time zone like so:

$dtz = new DateTimeZone('etc/GMT-10');
$dt = new DateTime(date("Y-m-d h:i A"), $dtz); 
$date = gmdate("Y-m-d h:i A", $dt->format('U'));
Lawrence DeSouza
  • 984
  • 5
  • 16
  • 34