SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation [Date] Formatting date + time zones

    There is a function: http://paste.ubuntu.com/21865

    It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as today, the function returns "Today". There is one problem. This function does not recognize time zones.

    How to adjust date to user's time zone? Is converting to timestamp() and then to format readable to visitors the one and only solution (e.g. strtotime() + date() OR DateTime object)? Perhaps, it's possible to solve this problem with mathematical operations (it needs more combinations). Has someone dealed with time zones?

    If I store timestamps instead of datetime, PHP would deal with time zones automatically (date() with 2nd parameter). However, it will be harder to display "Today", "Tomorrow", "Yesterday"...

  2. #2
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    Salem, OR
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not 100% clear in your question, but you can alter timezones in PHP via:

    http://www.php.net/manual/en/functio...mezone-set.php

    There are other methods of course (pun intended):

    http://www.php.net/manual/en/ref.datetime.php

    Does that help / answer your question?

  3. #3
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know that date_default_timezone_set() is very helpful if we use date() or strftime(). However, how to format date in proper time zone in my function? Is it possible to do it other way than: mktime() with date() OR strtotime() with date() OR storing timestamps in database (I prefer DATETIME)? Maybe some mathematic operations?

  4. #4
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Next problem. There is no gmtime() function in PHP - what a fault! Will PHP 6 introduce it? Current GMT timestamp may be achieved by: time() - date('Z') but maybe there are easier ways? Other method: date_default_timezone_set('GMT'). My CMS is multilingual. However, is it worth to store all times in GMT? Or better in local timezone?

    I must support MySQL and SQLite. Maybe I will add another SQL engine later.

    Okay, my questions are:
    1. Is it needed to convert date to timestamp?
    2. Should I generate date in SQL or in PHP?
    3. How to format date for the best performance? As INT or DATETIME?
    How about 2038?

    Effects that I must achieve:
    1. "5 minutes ago", "in 10 minutes"
    2. Date and time in timezone set by user
    3. "23.05.2008, 12:23" - format set by admin
    4. "Today, 12:23"

    I must support SQLite AND MySQL - so to format date in SQL, i would have to create functions for this purpose during connections or on demand - seperately for these engines.


Tags for this Thread

Bookmarks

Posting Permissions

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