note 37560 added to ref.datetime

From: Date: Wed, 19 Nov 2003 18:56:58 +0000
Subject: note 37560 added to ref.datetime
Groups: php.notes 
Request: Send a blank email to php-notes+get-60635@lists.php.net to get a copy of this message
A rectification to the some of the functions i posted a while ago. They do not work correctly under all circumstances (in my small test cases they worked) which is due to the fact that when you create a date using mktime, which returns a certain amount of seconds, this is not valid for every month since each month has a different amount of seconds. The solution is to break up the original timestamp, add to it's seperate parts and create a new timestamp. Old: ===== function sub($timestamp, $seconds,$minutes,$hours,$days,$months,$years) { $mytime = mktime(1+$hours,0+$minutes,0+$seconds,1+$months,1+$days,1970+$years); return $timestamp - $mytime; } function add($timestamp, $seconds,$minutes,$hours,$days,$months,$years) { $mytime = mktime(1+$hours,0+$minutes,0+$seconds,1+$months,1+$days,1970+$years); return $timestamp + $mytime; } ===== New: ===== function add($timestamp, $seconds, $minutes, $hours, $days, $months, $years) { $timePieces = getdate($timestamp); return mktime( $timePieces["hours"] + $hours, $timePieces["minutes"] + $minutes, $timePieces["seconds"] + $seconds, $timePieces["mon"] + $months, $timePieces["mday"] + $days, $timePieces["year"] + $years ); } function sub($timestamp, $seconds, $minutes, $hours, $days, $months, $years) { $timePieces = getdate($timestamp); return mktime( $timePieces["hours"] - $hours, $timePieces["minutes"] - $minutes, $timePieces["seconds"] - $seconds, $timePieces["mon"] - $months, $timePieces["mday"] - $days, $timePieces["year"] - $years ); } ===== Regards, - Vincent ---- Manual Page -- http://www.php.net/manual/en/ref.datetime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37560 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37560&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37560&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60635) next »