php.net |  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
 [2001-07-14 04:24 UTC] james at ncts dot zzn dot com
PHP Authors,

My names are James Hitz, author of http://jamhitz.tripod.com 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:

<?php

  function printdate($subject){

	$search = array( 
  		 "/[d]{4}/i",
		 "/[d]{3}/i",
		 "/[d]{2}/i",
		 "/[d]{1}/i",

		 "/[m]{4}/i",
		 "/[m]{3}/i",
		 "/[m]{2}/i",
		 "/[m]{1}/i",

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

	$replace = array(
		date("l"),
		date("D"),
		date("d"),
		date("j"),

		date("F"),
		date("M"),
		date("m"),
		date("n"),

		date("Y"),
		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: 

<QUOTE>
    date
    (PHP 3, PHP 4 )

    date -- Format a local time/date
    Description

    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
    string: 

    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" 

</QUOTE>

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 http://jamhitz.tripod.com.  All tutorials are free.

Thanking you.
James N. Hitz
james@ncts.zzn.com

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-16 06:01 UTC] joey@php.net
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:
Mon

You should be getting:
7on

Why?
Mon is becoming date("M")on, or something like it.
I don't know if this is what preg_replace SHOULD be
doing or not...it 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 http://www.joeysmith.com/phpdate.phps
 [2001-08-20 10:35 UTC] sniper@php.net
Should be closed..

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC