Results 1 to 8 of 8

Thread: php date/time

  1. #1
    Join Date
    Mar 2006
    Posts
    264

    Question php date/time

    in php you can set the date/time/time zone as explained here:

    http://nz2.php.net/datetime

    How does php know what the current time is though? Does it read the time on the server's clock or update via another internet service?

    And what if a php script was programmed with the USA timezone, and this script was installed on a server with the UK timezone, what timezone would php use?

  2. #2
    Join Date
    Jan 2008
    Location
    England
    Posts
    573
    Quote Originally Posted by ryan14 View Post
    How does php know what the current time is though? Does it read the time on the server's clock or update via another internet service?
    Yes it uses the servers clock. If you're using linux the clock can be updated/synched using ntpd.

    Quote Originally Posted by ryan14 View Post
    And what if a php script was programmed with the USA timezone, and this script was installed on a server with the UK timezone, what timezone would php use?
    Most developers use the time() function, then use the date() function to convert into whatever format/timezone is needed. The time() function calculates the timestamp in seconds since unix epoch (1/1/1970 00:00 GMT).

  3. #3
    Join Date
    Apr 2009
    Posts
    839
    sam250 is right, php (like almost all another applications) is using server time

  4. #4
    Join Date
    Mar 2006
    Posts
    264
    doesn't the clock in the CentOS operating system automatically update itself with the time servers?

    how would you display the time based on what country a user's ip address is from? Like if a person from the USA visited my website it will display the USA time, and if a person from the UK visited my website it will display the UK time etc etc FOR ALL COUNTRIES?


    Quote Originally Posted by sam250 View Post
    Yes it uses the servers clock. If you're using linux the clock can be updated/synched using ntpd.


    Most developers use the time() function, then use the date() function to convert into whatever format/timezone is needed. The time() function calculates the timestamp in seconds since unix epoch (1/1/1970 00:00 GMT).

  5. #5
    Join Date
    Jan 2008
    Location
    England
    Posts
    573
    Yes CentOS uses ntpd.

    1) You can use a geoIP script to lookup what country the visitor is from then lookup the timezone then use the date() function accordingly (most accurate).
    2) You could use JavaScript to get the visitors time and timezone but this wouldn't be as accurate as it would be based on the visitors clock, not the servers clock.
    3) Use a mix of 1 and 2, use JS to get the timezone but use php to get the timestamp.
    4) Get the language from the User Agent and convert to timezone then apply to the date function (not very accurate at all).

  6. #6
    Join Date
    Mar 2006
    Posts
    264
    thanks sam250. what if my website displayed when a video was uploaded on my USA server. EG:

    Video uploaded 25 August 5.02pm

    How would i display when the video was uploaded based on what country the ip address viewing it is from?

  7. #7
    Join Date
    Apr 2009
    Posts
    839
    Maybe something like "4 hours ago" will do the trick?

  8. #8
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    3,100
    I find timezones easiest to handle using MySQL built in capability.

    Here is more info on that and how to enable it: http://dev.mysql.com/doc/refman/5.1/...e-support.html

    When you add item to your database you save with it date/time that item was created.
    Code:
    insert into videos (name,created) values('some video' , NOW())
    This date/time is saved using server's timezone.

    When you wish to show this date/time to visitor you can show it in server's date/time and tell them what server's timezone is
    Code:
    select DATE_FORMAT(`created`,'%e/%c/%Y %H:%i') from videos
    or show it as some general timezone (i.e. GMT)
    Code:
    select DATE_FORMAT(CONVERT_TZ(`created`,'SYSTEM','GMT'),'%e/%c/%Y %H:%i') AS created from videos
    or you could show it in visitor's timezone.
    Code:
    select DATE_FORMAT(CONVERT_TZ(`created`,'SYSTEM','$users_timezone'),'%e/%c/%Y %H:%i') AS created from videos
    For the last you obviously need to know where your visitor is and what is their timezone. You can ask them about this or your could try to guess it, in which case you might find this useful: http://www.php.net/manual/en/book.geoip.php

Similar Threads

  1. PHP Time ... well date()
    By JustinSmall in forum Programming Discussion
    Replies: 7
    Last Post: 01-03-2009, 02:26 PM
  2. date/time displaying..
    By HakonHoy in forum Programming Discussion
    Replies: 5
    Last Post: 03-13-2006, 06:18 PM
  3. PHP time() from date()
    By sikkhost in forum Programming Discussion
    Replies: 2
    Last Post: 12-23-2005, 04:35 AM
  4. Date and Time Sync - NTP
    By phpjames in forum Hosting Security and Technology
    Replies: 2
    Last Post: 05-26-2002, 09:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •