php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #7705 sscanf does not zero pad decimal
Submitted: 2000-11-08 20:31 UTC Modified: 2000-11-08 20:41 UTC
From: jccann at home dot com Assigned:
Status: Closed Package: *General Issues
PHP Version: 4.0.3 OS: Windows NT
Private report: No CVE-ID: None
 [2000-11-08 20:31 UTC] jccann at home dot com
<?php
$Date = "06/15/2003";
sscanf( $Date, "%02d/%02d/%4d", &$DateMM, &$DateDD, &$DateYYYY );
print "date: $Date<br>";
print "MM: $DateMM<br>";
print "DD: $DateDD<br>";
print "YYYY: $DateYYYY<br>"
?>

produces this output:

Date: 06/15/2003
MM: 6
DD: 15
YYYY: 2003

Notice that for $DateMM, I specified '%02d", yet the parsed month is '6' and not '06'.

According to Example 1 in the PHP manual for sprintf
(http://www.php.net/manual/html/function.sprintf.html) the correct format for zero padded integers is:

sprintf ("%04d-%02d-%02d", $year, $month, $day);

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-11-08 20:41 UTC] derick@php.net
This is not a bug, you parsed the day with a %02d, zo the value is interpreted as an number, if you'd used:
sscanf( $Date, "%02s/%02s/%4d", &$DateMM, &$DateDD, &$DateYYYY ); it would be fine (because then it's parsed as a string).

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jun 14 00:01:33 2024 UTC