3

This is a PHP question. When I test this code

echo date("d-m-y h:i:S");

on my local server and on my hosted website (these two have different time zones) they return different datetime values,as expected. But when I try this code

echo mktime();

I see the same result on both servers.Does mktime() return the number of seconds from 1970 for a standard time zone,whatever your timezone is?

Thanks for your answers

user1800007
  • 45
  • 1
  • 5

3 Answers3

1

From the documentation:

mktime — Get Unix timestamp for a date

Synthesizing this with your initial question:

Does mktime() return the number of seconds from 1970 for a standard time zone,whatever your timezone is?

yep.

poitroae
  • 21,129
  • 10
  • 63
  • 81
  • So does this mean that,I can safely use mktime whatever server Im using,even if I move my web site from a host to another,because mktime is -kind of- zone safe?But i can not trust date() because it relies on local zones? – user1800007 Mar 08 '13 at 19:21
  • 1
    @user1800007 Yes exactly. You can rely on `mktime()` returning always the same across your servers. (Assumed the servers are configured correctly by your hoster which should be the case) – poitroae Mar 08 '13 at 19:22
  • Oh i think i see what you mean.You mean that the host admin should set the time correctly for his "time zone",mktime() will do the rest?Otherwise,if he/she set the time wrong for his timezone, mktime() will give us wrong values. – user1800007 Mar 08 '13 at 19:24
  • @user1800007 Yes, that's it! But this should be none of your business as the defaults are set correctly by default. – poitroae Mar 08 '13 at 19:27
  • Great! Yes it is not my business but its very important to be sure since my script will heavily depend on correct timestamp values,which is vital for the application.Thank you very much. (and thanks others ) – user1800007 Mar 08 '13 at 19:32
  • Definitely helped.Im amazed by the solution speed of stackoverflow and members. – user1800007 Mar 08 '13 at 19:35
1

mktime() returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

date() returns a string formatted according to the given format string using the given integer timestamp or the current time (local timezone) if no timestamp is given.

SeanWM
  • 16,789
  • 7
  • 51
  • 83
  • "...number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified." The time specified? I use mktime like this : mktime(). Without specifying anything. – user1800007 Mar 08 '13 at 19:28
  • 1
    You didn't even bother reading the documentation.. `Arguments may be left out in order from right to left; any arguments thus omitted will be set to the current value according to the local date and time.` – SeanWM Mar 08 '13 at 19:34
  • Sorry I checked after answering,its my mistake.Thanks for your answer but the docs made me confuse.It says "..according to the local date and time" which I suspect that if I omit the parameter it uses GMT as a standard parameter,not local date and time.This contradicts with second part of my question. – user1800007 Mar 08 '13 at 19:39
0

As the documentation mentions mktime() returns the seconds from begin of the epoch in GMT time while date() (without the second argument) returns the time in the local timezone.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266