0

I can't for the life of me figure why the unix timestamp returned from the code below shows up as Fri, 31 Aug 2012 06:26:00 GMT. I have tested this on my server as well as http://www.onlineconversion.com/unix_time.htm. The date/time returned should actually be Fri, 31 Aug 2012 02:26:41 PM.

$stringtime = strtotime(DATE("m/d/y G:i", STRTOTIME("2012-08-31 02:26:41")));

Can anyone tell me what I'm doing wrong please.

1
  • Guess the 41 seconds is just a miss spell? Commented Sep 1, 2012 at 1:11

4 Answers 4

4

Unix timestamps are always GMT (no time offset). You might need to set your offset either in your php.ini or in your code to allow for the 4 hour difference you are seeing.

Sign up to request clarification or add additional context in comments.

8 Comments

Yeah, the time difference seems about right for an EDT (GMT -4) time. Change the settings in your php.ini file on your server, or set the timezone using date_default_timezone_set. See here for documentation on that function.
@Chaser324 Thanks for adding the link in the comment, au.php.net seems to flaky as heck at the moment, was waiting for it to pop open to add the link to my answer. +1
No problem. The php.net site seems a bit flaky at the moment for me here in the US too.
It's not a timezone issue. The input time doesn't specify a time zone so it wouldn't convert it (it's assumed to be local). Look at his format string. It doesn't match the output. Its only using G for GMT formatted output
It is not correct to say UNIX timestamps are in GMT. They have no timezone at all. They are the number of seconds since a fixed point in time, which happens to be 1970-01-01T00:00:00Z.
|
0

You need to set the timezone first using date_default_timezone_set function, i.e.

date_default_timezone_set('America/Los_Angeles');

Manual.

Comments

0

strtotime() converts the input string to a unix timestamp. If your input string has no timezone information, PHP must guess at which timezone you mean. It doesn't really guess... it looks at your default time zone.

The UNIX timestamp is the number of seconds since January 1, 1970 UTC. So if you do this:

echo strtotime('1970-01-01 00:00:00');
// depends on your default time zone

you might expect zero. But you aren't specifying a timezone in the string, so it will use the default timezone. The actual output will vary.

date_default_timezone_set('UTC');
echo strtotime('1970-01-01 00:00:00');
// always 0

Now it will be zero. Or you can remove the ambiguity:

echo strtotime('1970-01-01 00:00:00 UTC');
// always 0

Likewise, when you use date(), PHP applies the default timezone when displaying the time.

The code in your question does not actually show where your error is. So I cannot really tell you what your problem is, other than you don't understand how PHP works with time stamps and time zones.

If your time stamp is correct, then you probably just need to use date_default_timezone_set() to set your timezone. That will affect all future calls to date().

You also may be interested in using the DateTime class, which is more straightforward than counting seconds from 1970.

Comments

-1

Don't use G. That converts it to Gmt and its the only format character its using. You just have the format string wrong

'D, d M Y h:i:s A'

$stringtime = date( 'D, d M Y h:i:s A' , strtotime("2012-08-31 02:26:41"));

1 Comment

G inside the string is nothing to do with GMT. G 24-hour format of an hour without leading zeros - 0 through 23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.