[development] problem with custom date and timezones

Seth Freach sfreach at gmail.com
Fri Jul 17 13:51:38 UTC 2009


a) mktime() will return a (GMT based) unix timestamp, but assumes that 
the params passed (namely, the 0 hour value) are local system time, thus 
they will be converted to GMT first.  the gmmktime() function returns 
the same value format, but assumes that the params are already in GMT, 
and will not do any further timezone shifting relative to local system 
settings.  You might want to play around with that function and GMT 
params.  NB: this is all at the PHP level, below the Drupal application 
level.  mktime() should work if your system's timezone offset is set to 
what you expect.  Is this hosted account somewhere or a machine you control?
roughly diagrammed:
storage: mktime('local clock info') -> filters through local system 
timezone offset -> GMT based unix timestamp
storage: gmmktime('GMT clock info') -> GMT based unix timestamp
display: GMT based unix timestamp -> format_date(...) -> 'local clock info'

b) storing dates is a design decision that you have to really evaluate 
from the start.  int's and unix timestamps are a reasonable approach 
though. check out http://drupal.org/node/262066 & 
http://drupal.org/node/291799 for more info.  The key with storing unix 
timestamps is to make sure that everything is stored in the db in a 
uniform timezone offset (ussually UTC).  Understand how Drupal deals 
with timezone handling at the application layer and don't try to 
"outsmart" it at the PHP layer.

c) if all date's have been stored uniformly as unix timestamps:
  $month = format_date($node->data_assentament, 'custom', 'm');
  $day = format_date($node->data_assentament, 'custom', 'd');
  $year = format_date($node->data_assentament, 'custom', 'Y');

The main idea here is that you have two layers of the application stack 
(PHP and Drupal) both trying to be smart about timezone handling, and 
each layer is undoing the other's efforts.  You have to make one be 
authoritative and the other passive from an application design 
perspective.  You will probably get the best results if you let Drupal 
be the authoritative layer when it comes to timezones.

Seth


Lluís wrote:
> I have set timezone to UTC in admin/settings/date-time and no timezone for user.
>
> So my problems are:
> a) How do I have to store the date? is mktime(0,0,0,$month,$day,$year); correct?
> b) If I want to use the date in views, how should I store it in
> database? I use int(11) now
> c) How can retrieve $month, $day, $year to make the form again?
>
>
> On Fri, Jul 17, 2009 at 2:11 PM, Seth Freach<sfreach at gmail.com> wrote:
>   
>> Lluís,
>>
>> time() will return a value relative to the epoch, which is epicentered in
>> GMT.
>> date() takes that value and returns a local time based on it.  IE, date()
>> knows about the computer's timezone and takes it into account.
>>
>> Drupal's format_date() function uses gmdate() internally, which, unlike
>> date(), does not do local timezone offsetting. gmdate() returns the formated
>> date string in GMT.  The reason format_date() does this is because it wants
>> to return a time that has meaning to the individual user's location, which
>> is not necessarily the computer's location, nor are all users always in the
>> same timezone.  Drupal does the timezone offsetting itself to allow a system
>> default timezone and individual user configurable timezones too.
>>
>> Double check your Drupal timezone settings at q=admin/settings/date-time and
>> possibly your user timezone settings at q=user/$uid/edit
>>
>> Seth
>>
>> Lluís wrote:
>>
>> Making some research my problem is with format date:
>>
>> $node->data_assentament -> 1247781600
>> date("d-m-Y H:i", $node->data_assentament) -> 17-07-2009 00:00
>> format_date($node->data_assentament,'medium') -> Dij, 07/16/2009 - 22:00
>>
>> (I need the date in dd/mm/YYYY format)
>>
>> Should I avoid format_date() or there is a better way to solve this?
>>
>> On Thu, Jul 16, 2009 at 5:40 PM, Lluís<enboig at gmail.com> wrote:
>>
>>
>> I have created a custom node type. I have a date field which I created
>> as int in order to use it in views, being able to sort it, format,
>> etc....
>>
>> My problem right now is that when inserting a node with one date, it
>> appears as "day before" when viewing the node. How should I save it to
>> avoid this problem?
>>
>> My form looks like
>>  $arrayF=getdate(($node->data_paper>0 ? $node->data_paper : time()));
>>  $arrayF['month']=$arrayF['mon'];
>>  $arrayF['day']=$arrayF['mday'];
>>  $form['data_paper_array'] = array(
>>    '#type' => 'date',
>>    '#title' => 'Data Factura',
>>    '#default_value' => $arrayF,
>>    '#required' => TRUE,
>>    '#weight' => -9,
>>  );
>>
>> My insert query looks like
>>  $dateF=$node->data_paper_array;
>>  $factura_tmsp=mktime(0,0,0,$dateF['month'],$dateF['day'],$dateF['year']);//
>>  $date['year']."-".$date['month']."-".$date['day'];
>>  $node->data_paper=$factura_tmsp;
>>  db_query("INSERT ......);
>>
>> And my theme function:
>>  $output.="<li><b>Data Factura:</b>
>> ".format_date($node->data_paper,'small')."</li>";
>>
>>
>> Any hint/advice? Thanks
>>
>> --
>> *La vida és com una taronja, què esperes a exprimir-la?
>> *Si creus que l'educació és cara, prova la ignorància.
>> *La vida és com una moneda, la pots gastar en el que vulguis però
>> només una vegada.
>> *Abans d'imprimir aquest missatge, pensa en el medi ambient.
>>
>>
>>
>>
>>     
>
>
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.drupal.org/pipermail/development/attachments/20090717/a7d41eae/attachment-0001.htm>


More information about the development mailing list