x
 
Create New Page
 View Previous Changes    ( Last updated by Dan Halbert )

Dates Explained

To understand how dates are used in EE and how to handle when writing modules there is help here:
* http://expressionengine.com/docs/development/usage/localization.html

And to help understand what is going on, I wrote this little template to help me:

<h1>Test of various time/date functions:</h1>

<
h3>Server</h3>
PHP date: <?php echo date('dMy h:i');?>(echo date('dMy h:i');)<br>
PHP gmdate: <?php echo gmdate('dMy h:i');?>(echo gmdate('dMy h:i');)<br>

<
h3>Database dates(GMT/UTC)</h3>
LOC now: <?php global $LOC; echo 'held in db as '.$LOC->now; echo ' which is '.date('dMy h:i',$LOC->now); ?> (date('dMy h:i',$LOC->now);)<br>

<
h3>User</h3>
Users timezone: <?php global $SESS; echo $SESS->userdata['timezone'] ; ?> ($SESS->userdata['timezone'])<br>
Users Time:<?php global $LOC; echo $LOC->set_human_time(); ?> ($LOC->set_human_time();)<br>


Which will output something like:

Test of various time/date functions:

Server
PHP date
: 24Feb06 08:13(echo date('dMy h:i');)
PHP gmdate: 24Feb06 04:13(echo gmdate('dMy h:i');)

Database dates(GMT/UTC)
LOC now: held in db as 1140826385 which is 24Feb06 04:13 (date('dMy h:i',$LOC->now);)

User
Users timezone
: UP1 ($SESS->userdata['timezone'])
Users Time:2006-02-24 17:13 ($LOC->set_human_time();)

Query that returns days since entry date:

select entry_date,round((unix_timestamp()-entry_date)/60/60/24) days_old from exp_weblog_titles

And another example, this time php code:

$dt='2006-30-3 15:00';
echo
'server now:'.date("dMy H:i")."\n";
echo
'gmt '.gmdate('dMy H:i')."\n";
echo
'local time now: '. $LOC->set_human_time( $LOC->now)."\n";
echo
'localised offset: '.round($LOC->set_localized_offset()/3600)." hours\n";
echo
'----'."\n";
echo
'as input: '.$dt."\n";
echo
'is in daylight saving? '.date('I',$dt)."\n";
echo
'input to gmt: '.$LOC->set_human_time($LOC->convert_human_date_to_gmt($dt)+ $LOC->set_localized_offset())."\n";

which outputs:

server now:01Mar06 14:11
gmt 01Mar06 22
:11
local time now
: 2006-03-02 02:11
localised offset
: -12 hours
----
as
input: 2006-30-3 15:00
is in daylight saving
? 0
input to gmt
: 2008-06-03 03:00

Note that server is 8 hours behine GMT and local time is 4 hours ahead of GMT and daylight saving does not apply.

Caution: The timestamp value stored in the database represents a GMT time, but it is not a standard Unix GMT timestamp. It is offset by the difference between the server’s time zone and GMT. Always use the Localization functions to convert between EE and Unix timestamps if you are reading or writing database timestamps directly. (As of EE 1.6.4 and before.)
Category:Dates

Categories: