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

Posted by AnrDaemon on June 21, 2008, 8:13 pm
Please log in for more thread options
Greetings, WebCM.
In reply to Your message dated Sunday, June 22, 2008, 01:57:51,

> 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.

So, please, correct me if I wrong...
You want to show textual representation of 3 nearby days, in user timezone?
It's really simple.
First, you must store meaningful info in database.
If you storing date - store it as date.
Second, retrieve data from database in a format you want to use in script.
You want UNIX timestamp? Convert date to it in SQL query.
Don't ask me "How?", read the f***ing manual. It's well explanatory.

To your solution.
1. Read PHP documentation, realize that you can set your script to work in
specified timezone. Key word is date_default_timezone_set
2. To detect, if we are "today/tomorrow/yesterday", just use simple match.
And remember, that there is NO way to have post entered "tomorrow", unless you
are hand-crafted it in database. But still...

<?php

// $post_time - subjected datetime
date_default_timezone_set(user timezone);
$today = (int)((time() + date('Z')) / 86400);
$post_day = (int)(($post_time + date('Z')) / 86400) - $today;

// Weird code begin here
if($post_day == -1)
{
$post_day = configLangGet('Yesterday');
}
else if($post_day == 1)
{
$post_day = configLangGet('Tomorrow');
}
else if($post_day == 0)
{
$post_day = configLangGet('Today');
}
else
{
$post_day = strftime(format, $post_time);
}
?>


--


Posted by Jerry Stuckle on June 21, 2008, 9:38 pm
Please log in for more thread options
AnrDaemon wrote:
> Greetings, WebCM.
> In reply to Your message dated Sunday, June 22, 2008, 01:57:51,
>
>> 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.
>
> So, please, correct me if I wrong...

You're wrong. As usual.


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


Posted by Jerry Stuckle on June 21, 2008, 10:19 pm
Please log in for more thread options
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. All you
know for sure is the timezone on the server. There is nothing in either
PHP or MySQL to get that. You will need to use a client-side language
such as JavaScript or a Java applet to get the client time.

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.

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 AnrDaemon on June 22, 2008, 11:17 am
Please log in for more thread options
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?

> 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?

> 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.


--


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