Page 1 of 1

Date handling, I've messed up with DST

Posted: Thu Oct 30, 2008 4:34 pm
by batfastad
Hi everyone
I've been developing a small intranet database system (PHP5/MySQL5) to replace our Filemaker contact management system. It's going well.
However there's one thing that I've never really got a handle on... date handling and timezones.
In MySQL I currently store all dates/times in Unix timestamp format - I'm not using MySQLs timestamp format.

I've currently noticed a significant weakness in my script, with regards to daylights saving time (GMT/BST). And I have some further questions about how to correctly handle time/date within PHP.
The server is set to automatically update the time from our IPCop firewall using NTP. The firewall syncs its time with 0.uk.pool.ntp.org and a few other backup servers, and it all worked well.
But at no point have I given PHP a timezone, or changed the timezone from GMT (UTC) to BST (UTC+1) when daylight savings kicks in.

There are several scripts where I get users to enter a date through separate text boxes, in dd mm yy format
Then when I save them into MySQL I use the following mktime function:

Code: Select all

$invoice_stamp = mktime(0, 0, 0, $invoice_date_m, $invoice_date_d, $invoice_date_y);
Where the variables are user-entered from separate text boxes

However since I'm based in the UK I suddenly noticed a system for logging exchange rates has suddenly shifted back by 1 month for the previous 6 months.
And the reason is that every record I'd added suddenly became 23:00 on the last day of the month, rather than 00:00 on the 1st of the month.

The solution to fix my data seems pretty straight forward, add an hour (3600 seconds) to each timestamp where the time is 23:00. Is that right?

Here's some questions:
1) Is there a php.ini setting / Linux timezone config I need to be changing when our server comes in/out of daylight savings?
Or should I just let the NTP daemon sync with my time server as it does currently?
The Ubuntu server is set to Europe/London timezone, and according to the 'date' section of phpinfo() default timezone is UTC, and the date.timezone directive shows no value for both local and master values

2) Should I be manually specifying the is_dst final parameter of the mktime() function? At the moment I omit that parameter, that's probably the cause of my problem :?
According to the man page of mktime it says this parameter is now deprecated and I should use PHPs timezone handling functions instead... anyone got any quick run through on when/how I should be using PHPs timezone functions to avoid this problem?

I know this is a really basic thing, but I'd really appreciate it if someone could run me through this - I really want to get this right! :D

Cheers, B

Re: Date handling, I've messed up with DST

Posted: Fri Oct 31, 2008 9:12 am
by dhrosti
Not sure about exact answers to your questions but there are a few websites that give you uk timestamps including daylight savings, etc. Might be useful for testing?

Re: Date handling, I've messed up with DST

Posted: Sun Nov 02, 2008 6:37 am
by batfastad
Yeah I've been using various timestamp generators to check, but I still don't know the best way to manage this.

How does everyone else manage the recording of dates and times in PHP apps, especially with respect to daylight savings?
I was under the impression that mktime would always generate a timestamp in GMT/UTC, so I'm finding this a bit strange.

And if it's advised to use PHPs timezone functions rather than the is_dst param of mktime(), what are those functions and how do I use them?

This is an extract of my phpinfo output: http://www.highbeeches.plus.com/phpinfodate.gif
Are there any Apache/PHP/Linux settings I should be changing when the server timezone drops in/out of daylight savings?

Thanks, B

Re: Date handling, I've messed up with DST

Posted: Mon Nov 03, 2008 12:34 pm
by batfastad
Anyone got any recommendations on the best way to store fixed dates?

EDIT: Should I actually be using the gmmktime() function instead of mktime?

This is a sort of block on my progress at the moment :(
Cheers, B