Date formatting

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a string saved in the format produced by date ('Ymd-Hms')



Can anyone suggest the easiest way to present this as e.g.

13.02pm, 15/Feb/2005


Things like strftime () don't seem to be applicable, and it's not
obviously how the general string formatting functions could be applied


Re: Date formatting

Nasty format... needs to be taken apart and put back in some parsable
format, such as this:


// example date in custom format...
$t_strCustomDate = "20050215-130257";

// disassemble...
$t_strYear = substr($t_strCustomDate, 0, 4);
$t_strMonth = substr($t_strCustomDate, 4, 2);
$t_strDay = substr($t_strCustomDate, 6, 2);
$t_strHour = substr($t_strCustomDate, 9, 2);
$t_strMinute = substr($t_strCustomDate, 11, 2);
$t_strSecond = substr($t_strCustomDate, 13, 2);

// reassemble...
$t_strCustomDate = $t_strYear . "-" . $t_strMonth . "-" . $t_strDay . "
" . $t_strHour . ":" . $t_strMinute . ":" . $t_strSecond;

// now it looks like this...
//$t_strCustomDate = "2005-02-15 13:02:57";

print date( ", d/M/Y ", strtotime($t_strCustomDate) );



Re: Date formatting

Martin Lucas-Smith wrote:
Quoted text here. Click to load it

$ts = preg_replace(
    " $1:$2:$3",

print date(", d/M/Y", strtotime($ts));


Re: Date formatting

Thanks for the replies:

Quoted text here. Click to load it

On Wed, 16 Feb 2005, Steve wrote:
Quoted text here. Click to load it

Basically the format had been chosen to append a timestamp to a flat-file
database (using the OS timestamp isn't possible because there could be
multiple instances of the same name). The format basically ensures that
the filenames will be in the correct alphabetical order, as the date is
effectively 'backwards'.

On Wed, 16 Feb 2005, Janwillem Borleffs wrote:

Quoted text here. Click to load it

Thanks - this use of preg_replace is an elegant solution which I'd


Re: Date formatting

On Wed, 16 Feb 2005 16:33:49 +0000, Martin Lucas-Smith wrote:

Quoted text here. Click to load it

Just wondering if you really want the month to be shown as the minute.
The minute indicator is i, m is for the numeric month.

So ,using the date and time of your post as the example (at the top)
 date('Ymd-Hms') gives 20050216-160249
 date('Ymd-His') gives 20050216-163349

You may really want the month but if not this will save you some bug
hunting :-)

Regards - Rodney Pont
The from address exists but is mostly dumped,
please send any emails to the address below
e-mail    ngpsm4 (at) infohitsystems (dot) ltd (dot) uk

Re: Date formatting

On Thu, 17 Feb 2005, Rodney Pont wrote:

Quoted text here. Click to load it

Thanks - yep, realised this yesterday, when the files started being
written out of sequence...


Site Timeline