note 37560 deleted from ref.datetime by felipe
| From: | felipe@php.net | Date: | Mon, 10 Dec 2007 17:42:50 +0000 |
| Subject: | note 37560 deleted from ref.datetime by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133190@lists.php.net to get a copy of this message | ||
Note Submitter: vincentv at thevoid dot demon dot nl
----
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