|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12161 The date() function
Submitted: 2001-07-14 04:24 UTC Modified: 2001-08-20 10:35 UTC
From: james at ncts dot zzn dot com Assigned:
Status: Closed Package: Date/time related
PHP Version: 4.0.6 OS: Windows 98
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: james at ncts dot zzn dot com
New email:
PHP Version: OS:


 [2001-07-14 04:24 UTC] james at ncts dot zzn dot com
PHP Authors,

My names are James Hitz, author of a website that provides free PHP tutorials.

I was creating a tutorial on user-defined functions and was working on one that accepts a string containing a date format (eg. dd-mmm-yy) and outputs  the date in the specified format.  The code is as follows:


  function printdate($subject){

	$search = array( 


		 "/[y]{1,2}/i" );

	$replace = array(


		date("y") );

 	$newdate =   preg_replace($search,$replace, $subject);
 	//return the formatted date
	return $newdate;


On my Win 98 (Second Edition) system running php version 4.06, I evoked the following above functrion using  the following code snippet:

    print printdate("dddd ddd, dd d m mm mmm mmmm y yy yyy yyyy");

This produced the following output:

    Thurs12a01 Thu, 12 12 7 07 Jul Jul01 01 01 2001 2001

Aparrently, I expected this to have produced something like this:

    Thursday Thu, 12 12 7 07 Jul July 01 01 2001 2001

The parsing of the date() function seems not to act 'normal'.  The documentation I have states as follows in regard to the date() function: 

    (PHP 3, PHP 4 )

    date -- Format a local time/date

    string date (string format [, int timestamp])

    Returns a string formatted according to the given  
    format string using the given timestamp or the current 
    local time if no timestamp is given. 

    The following characters are recognized in the format

    d - day of the month, 2 digits with leading zeros; i.e.
     "01" to "31" 

    D - day of the week, textual, 3 letters; i.e. "Fri" 

    F - month, textual, long; i.e. "January" 

    j - day of the month without leading zeros; i.e. "1"
        to "31" 

    l (lowercase 'L') - day of the week, textual, long;
       i.e. "Friday" 

    m - month; i.e. "01" to "12" 

    M - month, textual, 3 letters; i.e. "Jan" 

    n - month without leading zeros; i.e. "1" to "12" 

    r - RFC 822 formatted date; i.e. "Thu, 21 Dec 2000 16:01:07 +0200" 

    Y - year, 4 digits; i.e. "1999" 

    y - year, 2 digits; i.e. "99" 

    z - day of the year; i.e. "0" to "365" 


Is this a bug in the code or a bug in the documentation?  Seems to me like one of those. Please advise. I am making a tutorial that will make use of the above function and would appreciate if you would tell me what to tell my audience.

The tutorials I am make are very good for all - even complete beginners, so you can bundle them in your PHP distributions, or you can point users to  All tutorials are free.

Thanking you.
James N. Hitz


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-16 06:01 UTC]
This is not a PHP bug, it is a bug in your code, related to
the use of preg_replace.
Example: printdate("ddd");

You're expecting:

You should be getting:

Mon is becoming date("M")on, or something like it.
I don't know if this is what preg_replace SHOULD be
doing or seems to be making multiple passes
until it can no longer replace anything. Could someone
who knows about preg_replace please verify whether this
is the correct behavior or not?

In the meantime, I fixed the userland code so that it performs
as you expected. You can find it at
 [2001-08-20 10:35 UTC]
Should be closed..

PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Sep 23 12:01:27 2023 UTC