Modérateur PHPfrance |
10684 Messages
31 mars 2008, 16:25
Vérifies la valeur de retour de strtotime() lorsque tu lui spécifies une date qui n'existe pas (genre : 20080332). Son comportement a changé par rapport à php 4.
Quelques différences glanées dans les commentaires de la doc :
For those upgrading from PHP 4 to PHP 5 there are a number of things that are different about strtotime that I have NOT seen documented elsewhere, or at least not as clearly. I confirmed these with two separate fresh installations of PHP 4.4.1 and PHP 5.1.1.
1) Given that today is Tuesday: PHP4 "next tuesday" will return today. PHP5 "next tuesday" will actually return next tuesday as in "today +1week". Note that behavior has NOT changed for "last" and "this". For the string "last tuesday" both PHP4 and PHP5 would return "today -1week". For the string "this tuesday" both PHP4 and PHP5 would return "today".
2) You cannot include a space immediately following a + or - in PHP 5. In PHP4 the string "today + 1 week" works great. in PHP5 the string must be "today +1 week" to correctly parse.
3) (Partially noted in changelog.) If you pass php4 a string that is a mess ("asdf1234") it will return -1. If you in turn pass this to date() you'll get a warning like: Windows does not support dates prior to midnight. This is pretty useful for catching errors in your scripts. In PHP 5 strtotime will return FALSE which causes date() to return 12/31/69. Note that this is true of strings that might appear right such as "two weeks".
4) (Partially noted in changelog.) If you pass php4 an empty string it will error out with a "Notice: strtotime(): Called with empty time parameter". PHP5 will give no notice and return the current date stamp. (A much preferred behavior IMO.)
5) Some uppercase and mixed-case strings no longer parse correctly. In php4 "Yesterday" would parse correctly. In php5 "Yesterday" will return the infamous 1969 date. This is also true of Tomorrow and Today. [Red. This has been fixed in PHP already]
6. The keyword "previous" is supported in PHP5. (Finally!)
En gros, et dans la mesure ou cette fonction attends une date dans un format valide en paramètre, il semblerait logique qu'elle considère ta chaine comme un grand n'importe quoi et retourne la valeur FALSE. La fonction date te retourne alors le 1er janvier 70
Ceci étant, plutôt qu'utiliser une boucle et incrémenter un nombre sans te soucier de l'existance de la date obtenue, pourquoi ne pas profiter des spécificités de cette fonction pour incrémenter ta date d'un jour strtotime("+1 day"), et lui laisser le soin de gérer le changement de mois

Ce n'est pas en améliorant la bougie que l'on a inventé l'ampoule...