note 128463 added to function.strtotime
| From: | MarkAgius at markagius dot co dot uk | Date: | Mon, 08 May 2023 10:21:10 +0000 |
| Subject: | note 128463 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220418@lists.php.net to get a copy of this message | ||
Note:
If day of month is 12 or less, or year is entered as a two digit number and less than 31 or 12 then
you may get the wrong time value.
If you know the format used with the date string, then use the following code: (PHP version 5.5 or
later)
[code]
function getStrtotime($timeDateStr, $formatOfStr="j/m/Y"){
// Same as strtotime() but using the format $formatOfStr.
// Works with PHP version 5.5 and later.
// On error reading the time string, returns a date that never existed. 3/09/1752 Julian/Gregorian
calendar switch.
$timeStamp = DateTimeImmutable::createFromFormat($formatOfStr,$timeDateStr);
if($timeStamp===false){
// Bad date string or format string.
return -6858133619; // 3/09/1752
} else {
// Date string and format ok.
return $timeStamp->format("U"); // UNIX timestamp from 1/01/1970, 0:00:00 gmt
}
}
print date("j/m/Y", getStrtotime("1/02/2022", "j/m/Y"))."
Standard format. (j)<BR>\n";
print date("j/m/Y", getStrtotime("1/02/2022", "d/m/Y"))."
Standard format. (d)<BR>\n";
print date("j/m/Y", getStrtotime("1/02/2022", "j/m/y"))."
Standard format. (y) <-- Bad date as 2022 is not two digits.<BR>\n";
print date("j/m/Y", getStrtotime("21/02/2022", "j/m/Y"))."
Standard format. (j)<BR>\n";
print date("j/m/Y", getStrtotime("21/02/2022", "d/m/Y"))."
Standard format. (d)<BR>\n";
print date("j/m/Y", getStrtotime("2/01/2022", "m/j/Y"))." US
format.<BR>\n";
print date("j/m/Y", getStrtotime("2-01-22", "m-j-y"))." Two digit
year, US format. (Y)<BR>\n";
print date("j/m/Y", getStrtotime("2-01-22", "m-j-Y"))." Two digit
year, US format. (y) <-- Wrong year if two digits.<BR>\n";
print date("j/m/Y", getStrtotime("3/09/1752", "j/m/Y"))." No such
date.<BR>\n";
print date("j/m/Y", getStrtotime("50/00/19999", "j/m/Y"))." Bad
date string.<BR>\n";
print date("j/m/Y", getStrtotime("1-02-2022", "j/m/Y"))." Wrong -
or / used.<BR>\n";
[/code]
Output:
1/02/2022 Standard format. (j)
1/02/2022 Standard format. (d)
3/09/1752 Standard format. (y) <-- Bad date as 2022 is not two digits.
21/02/2022 Standard format. (j)
21/02/2022 Standard format. (d)
1/02/2022 US format.
1/02/2022 Two digit year, US format. (Y)
1/02/0022 Two digit year, US format. (y) <-- Wrong year if two digits.
3/09/1752 No such date.
3/09/1752 Bad date string.
3/09/1752 Wrong - or / used.
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.130 (proxied: 2a02:c7c:2a1f:d800:4c4d:afde:a8d8:cb2e)
----
Manual Page -- https://php.net/manual/en/function.strtotime.php
Edit -- https://main.php.net/note/edit/128463
Del: integrated -- https://main.php.net/note/delete/128463/integrated
Del: useless -- https://main.php.net/note/delete/128463/useless
Del: bad code -- https://main.php.net/note/delete/128463/bad+code
Del: spam -- https://main.php.net/note/delete/128463/spam
Del: non-english -- https://main.php.net/note/delete/128463/non-english
Del: in docs -- https://main.php.net/note/delete/128463/in+docs
Del: other reasons-- https://main.php.net/note/delete/128463
Reject -- https://main.php.net/note/reject/128463
Search -- https://main.php.net/manage/user-notes.php