<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.4" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Handling dates and times in PHP &#038; MySQL</title>
	<link>http://phpit.net/article/handling-date-time-php-mysql/</link>
	<description>PHPit has dozens of PHP articles, codesnippets and FAQ's.</description>
	<pubDate>Sun, 31 Aug 2008 11:11:57 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.4</generator>

	<item>
		<title>by: Tony Rabun</title>
		<link>http://phpit.net/article/handling-date-time-php-mysql/#comment-2965</link>
		<pubDate>Wed, 31 May 2006 17:53:29 +0000</pubDate>
		<guid>http://phpit.net/article/handling-date-time-php-mysql/#comment-2965</guid>
					<description>I agree that rewriting these libraries makes no sense, then again they get rewritten in every implementation of PHP...an indicator that no one likes using what is there. perhaps some object oriented code is due along the lines of .NET or Java? meanwhile putenv actually marshals data across process boundaries to get to the environment variables of the process in which your *webserver* is running. why would you do that just to format a date or time? it will slow your machine down and make your code dependent on local machine settings that you are changing for each and every script. in PHP5 use date_default_timezone_set to set the timezone for the current script instead of the whole machine. this ensures that your application does not hose up anything else and that you are not using resources marshalling across process boundaries</description>
		<content:encoded><![CDATA[<p>I agree that rewriting these libraries makes no sense, then again they get rewritten in every implementation of PHP&#8230;an indicator that no one likes using what is there. perhaps some object oriented code is due along the lines of .NET or Java? meanwhile putenv actually marshals data across process boundaries to get to the environment variables of the process in which your *webserver* is running. why would you do that just to format a date or time? it will slow your machine down and make your code dependent on local machine settings that you are changing for each and every script. in PHP5 use date_default_timezone_set to set the timezone for the current script instead of the whole machine. this ensures that your application does not hose up anything else and that you are not using resources marshalling across process boundaries
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Rawb</title>
		<link>http://phpit.net/article/handling-date-time-php-mysql/#comment-1129</link>
		<pubDate>Wed, 12 Apr 2006 22:20:51 +0000</pubDate>
		<guid>http://phpit.net/article/handling-date-time-php-mysql/#comment-1129</guid>
					<description>Please, whatever you do, do not follow this person's advice.

I am sure they had the best intentions in mind, but taking a project down this path is a surefire path into madness.

I have written code the exact way he suggested for many years, and in the end, it only produced pain. During daylight savings changes, all sorts of crazy things happen, because yesterday's date("Z") is not equal to today's. So when something happened yesterday at 3:30 PM, today it shows it as happening as 2:30 PM because of the hour difference.

You can store your data in your database as either a unix timestamp (recommended, because it is ALWAYS stored in GMT), or as a GMT timezone date string. When you pull it out, you will probably want to convert the date string into a timestamp, so be sure to use the gmmktime because you are feeding it a GMT timestamp.

From there on out, either use the putenv("TZ=some/timezone"); or in PHP 5.1 and newer: date_default_timezone_set("some/timezone"); and then just happily use the date(), mktime() and strtotime() functions as they were designed to be used.

Many man hours have been poured into various standard time handling libraries so that date() will always produce the correct date given the timezone and timestamp. Do not attempt to reimplement all of their work in a cheezy little library, you won't get it right, I promise. Again, I repeat, do not attempt to reproduce the pre-existing time handling functions, you will fail.

Please listen to my advice, it will save you hours and hours of pain sometime in the future.</description>
		<content:encoded><![CDATA[<p>Please, whatever you do, do not follow this person&#8217;s advice.</p>
<p>I am sure they had the best intentions in mind, but taking a project down this path is a surefire path into madness.</p>
<p>I have written code the exact way he suggested for many years, and in the end, it only produced pain. During daylight savings changes, all sorts of crazy things happen, because yesterday&#8217;s date(&#8221;Z&#8221;) is not equal to today&#8217;s. So when something happened yesterday at 3:30 PM, today it shows it as happening as 2:30 PM because of the hour difference.</p>
<p>You can store your data in your database as either a unix timestamp (recommended, because it is ALWAYS stored in GMT), or as a GMT timezone date string. When you pull it out, you will probably want to convert the date string into a timestamp, so be sure to use the gmmktime because you are feeding it a GMT timestamp.</p>
<p>From there on out, either use the putenv(&#8221;TZ=some/timezone&#8221;); or in PHP 5.1 and newer: date_default_timezone_set(&#8221;some/timezone&#8221;); and then just happily use the date(), mktime() and strtotime() functions as they were designed to be used.</p>
<p>Many man hours have been poured into various standard time handling libraries so that date() will always produce the correct date given the timezone and timestamp. Do not attempt to reimplement all of their work in a cheezy little library, you won&#8217;t get it right, I promise. Again, I repeat, do not attempt to reproduce the pre-existing time handling functions, you will fail.</p>
<p>Please listen to my advice, it will save you hours and hours of pain sometime in the future.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Dennis Pallett</title>
		<link>http://phpit.net/article/handling-date-time-php-mysql/#comment-1126</link>
		<pubDate>Wed, 12 Apr 2006 19:03:57 +0000</pubDate>
		<guid>http://phpit.net/article/handling-date-time-php-mysql/#comment-1126</guid>
					<description>DG: I'm afraid it doesn't work for PHP4 at all. I tried a couple of different things myself, but couldn't get it working properly. 

Pejalo: Are you sure about that? The time() on the PHPit server is quite different from my own localhost.

John: I'm actually saying you should store the original timestamp, and the gmt_timestamp. This allows the time to be formatted in the visitor's own timezone. Maybe you're talking about forms and filling in dates?

LoCo: By storing the date this way, you probably won't be able to use MySQL's inbuilt date functions (like DAY(), MONTH()) etc. This makes a lot harder to do date-based SQL queries.</description>
		<content:encoded><![CDATA[<p>DG: I&#8217;m afraid it doesn&#8217;t work for PHP4 at all. I tried a couple of different things myself, but couldn&#8217;t get it working properly. </p>
<p>Pejalo: Are you sure about that? The time() on the PHPit server is quite different from my own localhost.</p>
<p>John: I&#8217;m actually saying you should store the original timestamp, and the gmt_timestamp. This allows the time to be formatted in the visitor&#8217;s own timezone. Maybe you&#8217;re talking about forms and filling in dates?</p>
<p>LoCo: By storing the date this way, you probably won&#8217;t be able to use MySQL&#8217;s inbuilt date functions (like DAY(), MONTH()) etc. This makes a lot harder to do date-based SQL queries.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: LoCo</title>
		<link>http://phpit.net/article/handling-date-time-php-mysql/#comment-1100</link>
		<pubDate>Wed, 12 Apr 2006 00:47:02 +0000</pubDate>
		<guid>http://phpit.net/article/handling-date-time-php-mysql/#comment-1100</guid>
					<description>Recently I started to use this :)
I save date as int - YYYYMMDD... :)
and than  use sql substring function i retreive what i need 
I beleive that it is not good aproach when dealing with large database, but in this case it was pretty usefull and quick solution.</description>
		<content:encoded><![CDATA[<p>Recently I started to use this :)<br />
I save date as int - YYYYMMDD&#8230; :)<br />
and than  use sql substring function i retreive what i need<br />
I beleive that it is not good aproach when dealing with large database, but in this case it was pretty usefull and quick solution.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: John Josef</title>
		<link>http://phpit.net/article/handling-date-time-php-mysql/#comment-1082</link>
		<pubDate>Tue, 11 Apr 2006 00:59:31 +0000</pubDate>
		<guid>http://phpit.net/article/handling-date-time-php-mysql/#comment-1082</guid>
					<description>When storing information you should store raw data. Upon retrieving that data you should parse it (using the date() function). This information is a possibly hazardous practice when handling data because you are altering the timestamp information local to your host machine.</description>
		<content:encoded><![CDATA[<p>When storing information you should store raw data. Upon retrieving that data you should parse it (using the date() function). This information is a possibly hazardous practice when handling data because you are altering the timestamp information local to your host machine.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Pejalo</title>
		<link>http://phpit.net/article/handling-date-time-php-mysql/#comment-1079</link>
		<pubDate>Mon, 10 Apr 2006 22:28:24 +0000</pubDate>
		<guid>http://phpit.net/article/handling-date-time-php-mysql/#comment-1079</guid>
					<description>from page 1: "the timestamp stored in the database is based on the timezone of the server"
Is this accurate? For me, time() is returning the same value on servers located within different timezones. I only worry about timezones when I DISPLAY a time, in which I use gmdate() and add/subtract the seconds of time difference depending on the timezone I want the time to be formatted for.</description>
		<content:encoded><![CDATA[<p>from page 1: &#8220;the timestamp stored in the database is based on the timezone of the server&#8221;<br />
Is this accurate? For me, time() is returning the same value on servers located within different timezones. I only worry about timezones when I DISPLAY a time, in which I use gmdate() and add/subtract the seconds of time difference depending on the timezone I want the time to be formatted for.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: WoOzY</title>
		<link>http://phpit.net/article/handling-date-time-php-mysql/#comment-1076</link>
		<pubDate>Mon, 10 Apr 2006 16:32:35 +0000</pubDate>
		<guid>http://phpit.net/article/handling-date-time-php-mysql/#comment-1076</guid>
					<description>But yeah, what if I wanna show user friendly relative time? I usually do like this:

function relative_date($date)
{
	global $lang;

	$sec = time() - $date;
	switch(true) {
		case $sec </description>
		<content:encoded><![CDATA[<p>But yeah, what if I wanna show user friendly relative time? I usually do like this:</p>
<p>function relative_date($date)<br />
{<br />
	global $lang;</p>
<p>	$sec = time() - $date;<br />
	switch(true) {<br />
		case $sec
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: DG</title>
		<link>http://phpit.net/article/handling-date-time-php-mysql/#comment-1075</link>
		<pubDate>Mon, 10 Apr 2006 15:29:20 +0000</pubDate>
		<guid>http://phpit.net/article/handling-date-time-php-mysql/#comment-1075</guid>
					<description>Clarification: 
It's not working in PHP 4.4.1 / Apache 1.3.xx / Windows.
It works in PHP 5 / Apache 2 / Windows</description>
		<content:encoded><![CDATA[<p>Clarification:<br />
It&#8217;s not working in PHP 4.4.1 / Apache 1.3.xx / Windows.<br />
It works in PHP 5 / Apache 2 / Windows
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: DG</title>
		<link>http://phpit.net/article/handling-date-time-php-mysql/#comment-1074</link>
		<pubDate>Mon, 10 Apr 2006 15:21:37 +0000</pubDate>
		<guid>http://phpit.net/article/handling-date-time-php-mysql/#comment-1074</guid>
					<description>The putenv functionality is very nice:

// Europe/London
putenv('TZ=Europe/London');
$a_time = $gm_time + date('Z', time());
putenv('TZ=' . $original_tz);
echo 'Europe/London: ' . date($format, $a_time) . "";

But what can we do when working with PHP4?</description>
		<content:encoded><![CDATA[<p>The putenv functionality is very nice:</p>
<p>// Europe/London<br />
putenv(&#8217;TZ=Europe/London&#8217;);<br />
$a_time = $gm_time + date(&#8217;Z', time());<br />
putenv(&#8217;TZ=&#8217; . $original_tz);<br />
echo &#8216;Europe/London: &#8216; . date($format, $a_time) . &#8220;&#8221;;</p>
<p>But what can we do when working with PHP4?
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
