Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Obviously the leap to British Summer Time (Daylight Savings Time) is a big confusion in the world of programming, and I am indeed caught up in that confusion.</p> <p>The best possible solution I can find (which I will try to coherently explain) when using a timezone sensitive system is this:</p> <ul> <li>The Web Server and Database should both be running off the same machine timezone. I suggest UTC as it is the building blocks of timezone conversions. This will ensure that all of the dates stored in your database are constant, and don't skip any times such as the 1hour jump between Daylight Savings.</li> <li>At the top of all of your PHP scripts use <code>date_default_timezone_set('Europe/London');</code> with the specific timezone of the user.</li> <li><strike>When producing dates from user submitted forms, use <code>gmmktime();</code> to ensure that the timestamp created is UTC and not altered by the timezone that you have set.</strike></li> <li><code>date();</code> can be used when displaying dates, as this will convert the timestamp to the correct time taking into account the timezone that you have set.</li> <li><strike>If you do need to show a date in the UTC format then use <code>gmdate();</code> with the <code>$gm_timestamp</code> that you have taken from the database or created with <code>gmmktime();</code>.</strike></li> </ul> <p>I have written this bit of PHP to help understand the situation.</p> <pre><code>date_default_timezone_set('UTC'); $gmtime = gmmktime(2,0,0,03,29,2009); $time = mktime(2,0,0,03,29,2009); echo $gmtime.'&lt;br /&gt;'.date('r',$gmtime).'&lt;br /&gt;'.gmdate('r',$gmtime).'&lt;br /&gt;'; echo $time.'&lt;br /&gt;'.date('r',$time).'&lt;br /&gt;'.gmdate('r',$time).'&lt;br /&gt;'; date_default_timezone_set('Europe/London'); $gmtime = gmmktime(2,0,0,03,29,2009); $time = mktime(2,0,0,03,29,2009); echo $gmtime.'&lt;br /&gt;'.date('r',$gmtime).'&lt;br /&gt;'.gmdate('r',$gmtime).'&lt;br /&gt;'; echo $time.'&lt;br /&gt;'.date('r',$time).'&lt;br /&gt;'.gmdate('r',$time).'&lt;br /&gt;'; </code></pre> <p>Hopefully I've done a good job, but I doubt it because I'm still trying to battle the problem in my head.</p> <p><strong>UPDATE:</strong></p> <p>Glad I did this, because I am now having doubts about the user inputted dates.</p> <p>To get the User inputted date (taking into account their timezone) to match up with the UTC corresponding date in the database, you should put it through <code>mktime()</code>. And then use <code>gmdate('U', $timestamp);</code> to get the true UTC timestamp. (I think)</p> <p><strong><em>Example</em></strong></p> <p>Looking at it from a reporting side, the user is using the 'Europe/London' timezone. At the start of our PHP script we call <code>date_default_timezone_set('Europe/London');</code>, whilst the Database (and all the records within) is still in UTC.</p> <p>The user then sends through that they want to select a list of books added to the database between 25/03/2010 10:00 to 30/03/2010 14:00. The PHP script then runs the date variables through <code>mktime($hour, $minute, $second, $month, $day, $year)</code> to generate a correct UTC timestamp. The start date will not change, but PHP knows that the end date is within the BST timezone, so changes the timestamp to UTC accordingly.</p> <p>When the results are returned back to the user, <code>date('r', $date_added)</code> can be used to show the user the date the book was added to the database according to their set timezone.</p> <p>This link may help with understanding when it changes. <a href="http://www.daylightsavingtime.co.uk/" rel="nofollow noreferrer">http://www.daylightsavingtime.co.uk/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload