473,725 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4022
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_ti mezone_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_ti mezone_set(user timezone);
$today = (int)((time() + date('Z')) / 86400);
$post_day = (int)(($post_ti me + 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*******@free mail.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*******@attgl obal.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(date col, '%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*******@attgl obal.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(date col, '%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*******@free mail.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(date col, '%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*******@attgl obal.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_ti mezone_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_ti mezone_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*******@attgl obal.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_ti mezone_set('GMT ')? setlocale('en_U S')?
Jun 27 '08 #10

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

Similar topics

8
9442
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 $ Last-Modified: $Date: 2003/10/28 19:48:44 $ Author: A.M. Kuchling <amk@amk.ca> Status: Draft Type: Standards Track
5
2307
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 another time zone. It would be of great value if the time of some central source could be drawn, in the form of a ".js" file, into the script to avoid dependence on the client's date/time.
2
359
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 date is received, it's different than what had been entered in the client software. The client is in a different time zone than the server that the web service is located on and that's what the dates are different by - the time zone.
11
4673
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 having a heck of a time doing so. I have created a form where I use drop downs do specify year, month, date, hour, minute and seconds. When the form is loaded, the dropdowns have to display the proper values for the current time zone type. This
14
3684
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 of date formatting (RFC 822). I have been using a variation of RFC 1123 (just change the time zone to an offset, i.e. "-0800"). It seems to be writing okay, but it's failing to parse. I've tried changing the regional & language settings in my...
0
1418
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 a DataGrid with BoundColumn rows that are dates and times. Can one somehow in specifying the formatting for a date column say "date the bound date and treat it as UTC and convert it to PST when the page is rendered"? Or does one have to go thru...
13
16172
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 local time from UTC function formatToLocalTimeDate(inDate) { var today = new Date(); var inDateMod = new Date(inDate);
3
2083
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 dates are shifted due to time zone differences. Anyone know the best way to stop this ffrom happening?
44
10218
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
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9113
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.