note 41430 deleted from function.date by derick

From: Date: Sat, 22 May 2004 13:51:21 +0000
Subject: note 41430 deleted from function.date by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-69951@lists.php.net to get a copy of this message
Note Submitter: Jason Richard ---- I had a problem with my login script using PHP and MySQL when daylight savings time (DST) came around this year. I was using MYSQL NOW() function to add the current date and time to the user's record into a datetime field. When DST came into effect newly entered login times were an hour slow (I'm in EST). Since the last login is to be updated only if an hour or more has passed since the last login this was a big problem! The problem is that PHP takes DST into account and MySQL does not (as far as I know) and I was entering the time using MySQL's NOW() function and then comparing the value returned by PHP's time() function. A very simple solution to this is the following. Note the PHP time format string 'YmdHis' - it formats to YYYYMMDDHHMMSS which is what MySQL expects for a date/time field. $now = time(); $lastLogin = strtotime($row['lastLogin']); $diff = $now - $lastLogin; $now = date('YmdHis',$now) if($diff > 3600) { // 3600 seconds is 1 hour $query = 'UPDATE members SET logins = logins + 1, lastLogin = '.$now.' WHERE memberID = '.$SEC_ID; mysql_query($query); } Now the date entered is the PHP time (that accounts for DST) and we are comparing it to PHP time so all is well. I think this approach will work well for any time you wish to enter a date into MySQL using PHP. Just format the date using the "YmdHis" format string and use the strtotime() function to read a date retrieved from MySQL. The advantage to this approach rather than just entering the "normal" PHP date into a char or text field is that the dates are "human" readable in the table and all the MySQL date/time functions are available for future queries.

« previous php.notes (#69951) next »