473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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.
Jun 27 '08 #1
10 3978
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);
}
?>
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jun 27 '08 #2
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.
js*******@attglobal.net
==================

Jun 27 '08 #3
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.
js*******@attglobal.net
==================

Jun 27 '08 #4
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.

--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jun 27 '08 #5
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.
js*******@attglobal.net
==================

Jun 27 '08 #6
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.
Jun 27 '08 #7
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.
js*******@attglobal.net
==================

Jun 27 '08 #8
On 25 Jun, 10:24, WebCM <webcm...@gmail.comwrote:
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)."
Jun 27 '08 #9
Can you DIFFERENTIATE gmtime() and gmdate()? I've written clearly -
there is no >gmtime() << function. I'd like to get current UNIX
TIMESTAMP. Perhaps, it's possible by gmdate('U').

How about timezones? Should I store times in GMT in database or better
in local timezone for selected language (e.g. EN, DE, PL, EN/US)? My
CMS is multilingual. How should I achieve it?
date_default_timezone_set('GMT')? setlocale('en_US')?
Jun 27 '08 #10
Greetings, WebCM.
In reply to Your message dated Wednesday, June 25, 2008, 19:49:06,
Can you DIFFERENTIATE gmtime() and gmdate()? I've written clearly -
there is no >>gmtime() << function. I'd like to get current UNIX
TIMESTAMP. Perhaps, it's possible by gmdate('U').
Obviously,

gmdate('U') === date('U') === (string)time()

UNIXTIME has nothing to do with timezones.
It is what it is - amount of seconds passed from ....
RTFM.
How about timezones? Should I store times in GMT in database or better
in local timezone for selected language (e.g. EN, DE, PL, EN/US)? My
CMS is multilingual. How should I achieve it?
date_default_timezone_set('GMT')? setlocale('en_US')?
It's up to you. But I prefer to store dates as dates. Obviously, in my own
timezone, because database stored on my server.
That way, I will never run in time conflict, where reply beeing posted before
question.
How to deal with dates at display time - read my first answer in this thread.
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
5
by: Gord | last post by:
Many scripts and calendars call client side system time in order to make presentations. However, the client's time may be improperly set, if set at all, and/or the relevant time may be from...
2
by: lou | last post by:
When sending a date value to a web service, does .NET know what time zone that date is sent from? The reason I ask is because I have client software that sends data to our web service and when a...
11
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m...
14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
0
by: Randall Parker | last post by:
I realize that methods like DateTime.ToLocalTime() and DateTime.ToUniveralTime() exist. But suppose one wants to query out data from a database that has UTC times in it. Suppose one wants to have...
13
by: maflatoun | last post by:
Hi, I have the following function to convert UTC time to Local time. It works perfect for GMT- (Minus) time zones however it provides incorrect results for GMT+(Plus) time zones? // Format to...
3
by: C Villalba | last post by:
Birth Date and a Purchase Date aata is being stored in sql. This information is retrieved across time zones through a .Net Web Service. When the data is viewed in a .Net client application these...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectóplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.