cancel
Showing results for 
Search instead for 
Did you mean: 

PHP mktime, daylight savings, timezones best practice?

batfastad
Grafter
Posts: 85
Registered: ‎27-09-2007

PHP mktime, daylight savings, timezones best practice?

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 MySQL's timestamp format (yet).
I've currently noticed a big problem 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 Linux server is set to automatically update the time from our IPCop firewall using NTP. The firewall then syncs its time with uk.pool.ntp.org and a few other backup servers, and it all works 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:
$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 my 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.
I assumed that timestamps would always be returned in GMT/UTC, but obviously I've misunderstood that, or made it up Undecided
The solution to fix my data seems pretty straight forward, add an hour (3600 seconds) to each timestamp where the time is 23:00. Would that be 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! Grin
Cheers, B