Click here to get back home

DATE formatting and TIME zones - how to deal with this hell?

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
DATE formatting and TIME zones - how to deal with this hell? WebCM 06-21-2008
Get Chitika Premium
Posted by Jerry Stuckle on June 22, 2008, 2:29 pm
Please log in for more thread options
AnrDaemon wrote:
> Greetings, Jerry Stuckle.
> In reply to Your message dated Sunday, June 22, 2008, 06:19:16,
>
>> WebCM wrote:
>>> There is a function: http://paste.ubuntu.com/21865
>>>
>>> It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: [b]datetime[/
>>> b]. 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 stored 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"...
>>>
>>> In conclusion, this function must return formatted string in proper
>>> time zone. It must return "Today" and optionally "2 minutes ago" when
>>> needed. How to do it? Is it better to make mathematical operations? Or
>>> should I use build-in functions like: mktime(), strftime(), date(),
>>> strtotime()...? Everything must be very FAST.
>>>
>
>> First of all, you don't necessarily know the client's timezone.
>
> Forums typically has such configuration option for registered users.
> How can you missed this simple point?
>

Which has absolutely nothing to do with the ops question - or the
problem at hand. How can you miss this simple point? Oh, yes. I
forgot who you are. Now I know how you could miss something so simple.

>> To use such a function, you need have access to the date/time stored in
>> the database. Storing as a datetime or timestamp has it's advantages -
>> you can retrieve it in a number of formats, such as UNIX_TIMESTAMP or
>> any number of other formats. It also makes it easy to sort on this
>> field. For instance, to get the format you need would be quite easy
>> with MySQL's DATEFORMAT function, i.e.
>
>> DATEFORMAT(datecol, '%Y-%m-%d %H:%i:%s');
>
>> Or, if the column is in unixtime, a simple FROM_UNIXTIME() call works.
>
> Will it return "today" or "yesterday" as OP requested?
>

That is not what the op requested. Learn to read.

>> The point is - there are a lot of options here - all of which work.
>> Don't restrict yourself to PHP - look at the database functions, also.
>
>> As for fast - this is not the time to be worried about speed - that's
>> over-optimizing. Rather, if it's a problem, later you can look at your
>> code and determine how to speed it up. Unless you write some awfully
>> inefficient code, this conversion will NOT be your problem.
>
>


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


Posted by WebCM on June 25, 2008, 5:24 am
Please log in for more thread options
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.

Posted by Jerry Stuckle on June 25, 2008, 6:59 am
Please log in for more thread options
WebCM wrote:
> 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.
>

I don't know about sqllite - I don't use it for anything but very
trivial stuff. I use MySQL for everything else - and all of this can be
done in MySQL.

As to storing - if you need a date, use a date type. But if you need a
date and a time, use a date/time value, such as a timestamp. And you
can do most everything you want in MySQL. See comp.databases.mysql for
more information.

As for your CMS supporting both - personally, I wouldn't worry about it.
Look at the other CMS's - most use MySQL; a few use PostGresSQL. None
use sqllite - it's really not suitable for heavy duty database
applications (hence the term sql *lite*).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


Posted by Peter H. Coffin on June 25, 2008, 10:18 am
Please log in for more thread options
On Wed, 25 Jun 2008 02:24:27 -0700 (PDT), WebCM wrote:
> 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?

What's hard about gmdate('h:i:s') or something similar? "date()" doesn't
mean just date-part, it can also mean time-part.

--
The Web brings people together because no matter what kind of a twisted
sexual mutant you happen to be, you've got millions of pals out there.
Type in 'Find people that have sex with goats that are on fire' and the
computer will ask, 'Specify type of goat.' -- Rich Jeni

Posted by Captain Paralytic on June 25, 2008, 10:47 am
Please log in for more thread options
> Next problem. There is no gmtime() function in PHP - what a fault!

Nearly correct. What you should have said is:
"Next problem. I am totally unable to read what is written in the
manual - what a fault!

I quote: "Identical to the date() function except that the time
returned is Greenwich Mean Time (GMT)."

Similar ThreadsPosted
2 different time zones September 29, 2004, 9:54 pm
Displaying time in different time zones May 11, 2005, 11:52 am
OT: Looking for data (.csv?) that associates zip codes with time zones July 2, 2005, 6:36 am
Date formatting February 16, 2005, 5:33 pm
Php Date-Formatting Help January 5, 2006, 6:18 pm
formatting time April 28, 2006, 2:49 am
PHP time formatting ? May 15, 2008, 11:16 am
Formatting a date with php from a mysql table October 27, 2007, 3:57 pm
Formatting date returned from MySQL query February 22, 2006, 8:57 am
formatting time string from: 2007-05-30 11:46:34 June 6, 2007, 11:10 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap