PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > bad usage of strtotime?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
bad usage of strtotime?

Réponse
 
LinkBack Outils de la discussion
Vieux 24/04/2008, 09h47   #1
Erwin Moller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut bad usage of strtotime?

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($date1);
$UTSdate2 = strtotime($date2);

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
Citation:
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 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($date1,$now);
$UTSdate2 = strtotime($date2,$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
  Réponse avec citation
Vieux 24/04/2008, 09h49   #2
Erwin Moller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad usage of strtotime?

Erwin Moller schreef:

<snip typo correction)

> Would this be better?
> $UTSnow = time();
> $UTSdate1 = strtotime($date1,$now);
> $UTSdate2 = strtotime($date2,$now);
>


should read of course:
$UTSnow = time();
$UTSdate1 = strtotime($date1,$UTSnow);
$UTSdate2 = strtotime($date2,$UTSnow);

Erwin
  Réponse avec citation
Vieux 24/04/2008, 10h00   #3
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad usage of strtotime?

On Thu, 24 Apr 2008 10:49:41 +0200, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote:

> Erwin Moller schreef:
>
> <snip typo correction)
>
>> Would this be better?
>> $UTSnow = time();
>> $UTSdate1 = strtotime($date1,$now);
>> $UTSdate2 = strtotime($date2,$now);
>>

>
> should read of course:
> $UTSnow = time();
> $UTSdate1 = strtotime($date1,$UTSnow);
> $UTSdate2 = strtotime($date2,$UTSnow);


Giving an explicit date will render the 'relative' part useless:
<?php
date_default_timezone_set('Europe/Amsterdam');
$date = "2008-02-23";
$UTSdate1 = strtotime($date);
sleep(2);
$UTSdate2 = strtotime($date);
var_dump($UTSdate1,$UTSdate2);
?>
Output:
int(1203721200)
int(1203721200)

However, giving a relative date, they can differ:
<?php
date_default_timezone_set('Europe/Amsterdam');
$date = "-2 hours";
//without supplying relative to what:
$UTSdate1 = strtotime($date);
sleep(2);
$UTSdate2 = strtotime($date);
var_dump($UTSdate1,$UTSdate2);
//with supplying a previously created timestamp:
$now = time();
$UTSdate1 = strtotime($date,$now);
sleep(2);
$UTSdate2 = strtotime($date,$now);
var_dump($UTSdate1,$UTSdate2);
?>
int(1209020255)
int(1209020257)
int(1209020257)
int(1209020257)

So you only have a problem with 'relative' datestrings ( +/- N hours,
'last Saturday', etc.). Specific datestrings are not relative to anything
in this context, so will yield the same output.
--
Rik Wasmus
  Réponse avec citation
Vieux 24/04/2008, 10h03   #4
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad usage of strtotime?

On Thu, 24 Apr 2008 11:00:45 +0200, Rik Wasmus
<luiheidsgoeroe@hotmail.com> wrote:

> On Thu, 24 Apr 2008 10:49:41 +0200, Erwin Moller
> <Since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote:
>
>> Erwin Moller schreef:
>>
>> <snip typo correction)
>>
>>> Would this be better?
>>> $UTSnow = time();
>>> $UTSdate1 = strtotime($date1,$now);
>>> $UTSdate2 = strtotime($date2,$now);
>>>

>>
>> should read of course:
>> $UTSnow = time();
>> $UTSdate1 = strtotime($date1,$UTSnow);
>> $UTSdate2 = strtotime($date2,$UTSnow);

>
> Giving an explicit date will render the 'relative' part useless:
> <?php
> date_default_timezone_set('Europe/Amsterdam');
> $date = "2008-02-23";
> $UTSdate1 = strtotime($date);
> sleep(2);
> $UTSdate2 = strtotime($date);
> var_dump($UTSdate1,$UTSdate2);
> ?>
> Output:
> int(1203721200)
> int(1203721200)
>
> However, giving a relative date, they can differ:
> <?php
> date_default_timezone_set('Europe/Amsterdam');
> $date = "-2 hours";
> //without supplying relative to what:
> $UTSdate1 = strtotime($date);
> sleep(2);
> $UTSdate2 = strtotime($date);
> var_dump($UTSdate1,$UTSdate2);
> //with supplying a previously created timestamp:
> $now = time();
> $UTSdate1 = strtotime($date,$now);
> sleep(2);
> $UTSdate2 = strtotime($date,$now);
> var_dump($UTSdate1,$UTSdate2);
> ?>
> int(1209020255)
> int(1209020257)
> int(1209020257)
> int(1209020257)
>
> So you only have a problem with 'relative' datestrings ( +/- N hours,
> 'last Saturday', etc.). Specific datestrings are not relative to
> anything in this context, so will yield the same output.


And on a sidenote: depending on the circumstances, I usually compare
timestamps which have to match within a margin of error (kind of like
float comparisons):

if($date1 - $error < $date2 < $date1 + $error){
//equal enough for this comparison
} else {
//outside margin of error
}
--
Rik Wasmus
  Réponse avec citation
Vieux 24/04/2008, 10h24   #5
Erwin Moller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad usage of strtotime?

Rik Wasmus schreef:
> On Thu, 24 Apr 2008 11:00:45 +0200, Rik Wasmus
> <luiheidsgoeroe@hotmail.com> wrote:
>
>> On Thu, 24 Apr 2008 10:49:41 +0200, Erwin Moller
>> <Since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote:
>>
>>> Erwin Moller schreef:
>>>
>>> <snip typo correction)
>>>
>>>> Would this be better?
>>>> $UTSnow = time();
>>>> $UTSdate1 = strtotime($date1,$now);
>>>> $UTSdate2 = strtotime($date2,$now);
>>>>
>>>
>>> should read of course:
>>> $UTSnow = time();
>>> $UTSdate1 = strtotime($date1,$UTSnow);
>>> $UTSdate2 = strtotime($date2,$UTSnow);

>>
>> Giving an explicit date will render the 'relative' part useless:
>> <?php
>> date_default_timezone_set('Europe/Amsterdam');
>> $date = "2008-02-23";
>> $UTSdate1 = strtotime($date);
>> sleep(2);
>> $UTSdate2 = strtotime($date);
>> var_dump($UTSdate1,$UTSdate2);
>> ?>
>> Output:
>> int(1203721200)
>> int(1203721200)
>>
>> However, giving a relative date, they can differ:
>> <?php
>> date_default_timezone_set('Europe/Amsterdam');
>> $date = "-2 hours";
>> //without supplying relative to what:
>> $UTSdate1 = strtotime($date);
>> sleep(2);
>> $UTSdate2 = strtotime($date);
>> var_dump($UTSdate1,$UTSdate2);
>> //with supplying a previously created timestamp:
>> $now = time();
>> $UTSdate1 = strtotime($date,$now);
>> sleep(2);
>> $UTSdate2 = strtotime($date,$now);
>> var_dump($UTSdate1,$UTSdate2);
>> ?>
>> int(1209020255)
>> int(1209020257)
>> int(1209020257)
>> int(1209020257)
>>
>> So you only have a problem with 'relative' datestrings ( +/- N hours,
>> 'last Saturday', etc.). Specific datestrings are not relative to
>> anything in this context, so will yield the same output.

>
> And on a sidenote: depending on the circumstances, I usually compare
> timestamps which have to match within a margin of error (kind of like
> float comparisons):
>
> if($date1 - $error < $date2 < $date1 + $error){
> //equal enough for this comparison
> } else {
> //outside margin of error
> }


Thanks a lot Rik.
Very clear explanation. :-)

My confusion was clearly based on my total misunderstanding of the
'relative to' part, which was about a passed string like '+5 hours'.

Confusion cleared up now, and I feel a bit stupid too for asking. ;-)

Regards,
Erwin Moller
  Réponse avec citation
Vieux 24/04/2008, 10h37   #6
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad usage of strtotime?

On 24 Apr, 09:47, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.com> wrote:
> I think not after rereading the manual:
>
> FROM:http://nl2.php.net/manual/en/function.strtotime.php
>
Citation:
> 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.
>

Hi Erwin,
in another recent thread I mentioned that the statement "The function
expects to be given a string containing a US English date format" is
contradicted later in the same page.
  Réponse avec citation
Vieux 24/04/2008, 10h55   #7
Erwin Moller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad usage of strtotime?

Captain Paralytic schreef:
> On 24 Apr, 09:47, Erwin Moller
> <Since_humans_read_this_I_am_spammed_too_m...@spam yourself.com> wrote:
>> I think not after rereading the manual:
>>
>> FROM:http://nl2.php.net/manual/en/function.strtotime.php
>>
Citation:
>> 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.
>>

> Hi Erwin,
> in another recent thread I mentioned that the statement "The function
> expects to be given a string containing a US English date format" is
> contradicted later in the same page.


Hi,

Yes, I can hardly say '+5 days' is an English date format.

Since we both get confused by this entry in the manual, I suggest
somebody rewrites it.
Or, of course, we both suck and should find another job than programming
PHP. ;-)
Anyway, Rik cleared up my misunderstanding of that relative part, and I
think I keep programming PHP.

Regards,
Erwin Moller
  Réponse avec citation
Vieux 24/04/2008, 11h14   #8
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad usage of strtotime?

On Thu, 24 Apr 2008 11:55:08 +0200, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote:

> Captain Paralytic schreef:
>> On 24 Apr, 09:47, Erwin Moller
>> <Since_humans_read_this_I_am_spammed_too_m...@spam yourself.com> wrote:
>>> I think not after rereading the manual:
>>>
>>> FROM:http://nl2.php.net/manual/en/function.strtotime.php
>>>
Citation:
>>> 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.
>>>

>> Hi Erwin,
>> in another recent thread I mentioned that the statement "The function
>> expects to be given a string containing a US English date format" is
>> contradicted later in the same page.

>
> Yes, I can hardly say '+5 days' is an English date format.


Well, '+5 dagen' or '+5 jours' is not understood, so I'd say it's english,
allthough not a date format :P

> Since we both get confused by this entry in the manual, I suggest
> somebody rewrites it.
> Or, of course, we both suck and should find another job than programming
> PHP. ;-)
> Anyway, Rik cleared up my misunderstanding of that relative part, and I
> think I keep programming PHP.


The manual of strtotime can be confusing yes. When in doubt, I just test,
which leads me to believe i should just take it as 'strtotime will expect
a dd/dd[/dd[dd]] string to be in US format' (and for instance not
dd-dd-dddd):
echo strtotime('02-03-1980');//320799600, 1980-03-02
echo strtotime('02/03/1980');//318380400, 1980-02-03
echo strtotime('02-03');//unrecognized, 1970-01-01
echo strtotime('02/03');//US, 2008-02-03

Indeed not clear from the manual, just trial and error...
--
Rik Wasmus
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h31.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16162 seconds with 16 queries