note 109120 added to function.strptime
| From: | emanuil dot tolev at gmail dot com | Date: | Thu, 21 Jun 2012 16:09:02 +0000 |
| Subject: | note 109120 added to function.strptime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188738@lists.php.net to get a copy of this message | ||
Be careful: the output of strptime() ( http://www.php.net/manual/en/function.strptime.php
) cannot always be used with mktime() ( http://www.php.net/manual/en/function.mktime.php
)!
This is not because of what platform you're using or what format strings glibc supports. This
is simply because strptime returns years SINCE 1900 (as documented above) and mktime expects a year
in the format returned by date('Y') - which is the full 4 digits.
Therefore, if you parse a date with strptime and want to give it to mktime, you have to pass in
($parsed_time['tm_year'] + 1900) as the year parameter to mktime(), not just
$parsed_time['tm_year'].
This issue arose when I had a date like: 19/06/2012 12:03:34. strtotime() doesn't parse this
particular format, so I needed custom parsing. So I ended up with:
1. $ts = $service->getNeededDateTime();
2. $ts = strptime($ts, '%d/%m/%Y %H:%M:%S');
3. $ts = mktime($ts['tm_hour'], $ts['tm_min'], $ts['tm_sec'],
$ts['tm_mon'], $ts['tm_mday'], ($ts['tm_year'] + 1900));
----
Server IP: 69.147.83.197
Probable Submitter: 144.124.16.33
----
Manual Page -- http://www.php.net/manual/en/function.strptime.php
Edit -- https://master.php.net/note/edit/109120
Del: integrated -- https://master.php.net/note/delete/109120/integrated
Del: useless -- https://master.php.net/note/delete/109120/useless
Del: bad code -- https://master.php.net/note/delete/109120/bad+code
Del: spam -- https://master.php.net/note/delete/109120/spam
Del: non-english -- https://master.php.net/note/delete/109120/non-english
Del: in docs -- https://master.php.net/note/delete/109120/in+docs
Del: other reasons-- https://master.php.net/note/delete/109120
Reject -- https://master.php.net/note/reject/109120
Search -- https://master.php.net/manage/user-notes.php