Hi group,
I have been using strtotime a lot in my code.
I wonder if I made a mistake in my thinking. :-/
Here follows a stripped down example.
consider some dates:
$date1 = "2008-02-23";
$date2 = "2008-02-23";
(They are the same)
Suppose I need to know if date1 is smaller/equal or bigger than date2.
I often used code like:
$UTSdate1 = strtotime($date 1);
$UTSdate2 = strtotime($date 2);
if ($UTSdate1 <= $UTSdate2){
// date1 before or equal to date2
} else {
// date1 after date2
}
Good, not?
I think not after rereading the manual:
FROM: http://nl2.php.net/manual/en/function.strtotime.php
The part "relative to the timestamp given in now" got me confused.
My question: Since the strtotime() is calculating 2 times the Unix Time
Stamp, do I risk that during execution of my script the second is
increased between the first call to strtotime() and the second, thus
making my above approach invalid?
Would this be better?
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$now);
$UTSdate2 = strtotime($date 2,$now);
Should I change my code according to the second approach (using $UTSnow)?
I know chances of ticking a second are small in such a small piece of
code, but I don't want to deliver code that 'works most of the time'.
Thanks for any insights.
Regards,
Erwin Moller
I have been using strtotime a lot in my code.
I wonder if I made a mistake in my thinking. :-/
Here follows a stripped down example.
consider some dates:
$date1 = "2008-02-23";
$date2 = "2008-02-23";
(They are the same)
Suppose I need to know if date1 is smaller/equal or bigger than date2.
I often used code like:
$UTSdate1 = strtotime($date 1);
$UTSdate2 = strtotime($date 2);
if ($UTSdate1 <= $UTSdate2){
// date1 before or equal to date2
} else {
// date1 after date2
}
Good, not?
I think not after rereading the manual:
FROM: http://nl2.php.net/manual/en/function.strtotime.php
int strtotime ( string $time [, int $now ] )
The function expects to be given a string containing a US English date
format and will try to parse that format into a Unix timestamp (the
number of seconds since January 1 1970 00:00:00 GMT), relative to the
timestamp given in now , or the current time if now is not supplied.
The function expects to be given a string containing a US English date
format and will try to parse that format into a Unix timestamp (the
number of seconds since January 1 1970 00:00:00 GMT), relative to the
timestamp given in now , or the current time if now is not supplied.
My question: Since the strtotime() is calculating 2 times the Unix Time
Stamp, do I risk that during execution of my script the second is
increased between the first call to strtotime() and the second, thus
making my above approach invalid?
Would this be better?
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$now);
$UTSdate2 = strtotime($date 2,$now);
Should I change my code according to the second approach (using $UTSnow)?
I know chances of ticking a second are small in such a small piece of
code, but I don't want to deliver code that 'works most of the time'.
Thanks for any insights.
Regards,
Erwin Moller
Comment