Jump to content

Archived

This topic is now archived and is closed to further replies.

paulspoon

Date Format Help

Recommended Posts

I've got a text field with the following value "20060710" and want to convert it to look like this "2005/07/10".

All help appreciated

Share this post


Link to post
Share on other sites
I suppose the date is not generated but inputted by the costumers. You could use:

[code]
<?php
  $notGood = "20060710";
  $match = array();
  if(preg_match('/^([0-9]){4}([0-9]){2}([0-9]){2})/', $notGood, $match)) {
    $good = $match[0] . "/" . $match[1] . "/" . $match[2];
  }

  echo notGood . " - " . $good;
?>
[/code]

Or you could use following aswell:
[code]
<?php
  $notGood = "20060710";
  $match = array();
  if(preg_match('/^([0-9]){4}([0-9]){2}([0-9]){2})/', $notGood, $match)) {
    $good = date('YYYY/m/d', mktime('', '', '', $match[1], $match[2], $match[0]));
  }
  echo $good;
?>
[/code]

Share this post


Link to post
Share on other sites
You can change it to

[code]
<?php
    if(preg_match('/^(\d){4}(\d){2}(\d){2}/', $notGood, $match)) {
        ....
    }
?>
[/code]

Share this post


Link to post
Share on other sites
Still nothing , next error

Parse error: syntax error, unexpected T_STRING in /Data/www/dev.cha

Sorry for the inconvinience

Share this post


Link to post
Share on other sites
Just tested it and is working:

[code]
<?php
  $notGood = "20060710";
  $match = array();
  $good = "";
  if(preg_match('/^([0-9]){4}([0-9]){2}([0-9]){2}/', $notGood, $match)) {
    $good = date('Y/m/d', mktime('', '', '', $match[1], $match[2], $match[0]));
  }
  echo $good;
?>
[/code]

Also working:
[code]
<?php
  $notGood = "20060710";
  $match = array();
  if(preg_match('/^([0-9]){4}([0-9]){2}([0-9]){2}/', $notGood, $match)) {
    $good = $match[0] . "/" . $match[1] . "/" . $match[2];
  }

  echo $notGood . " - " . $good;
?>
[/code]

Sorry, was forgotten some $ signs and had a ) sign too much in.

Share this post


Link to post
Share on other sites
$datefromdb = $sominhere['time_data'];
$year = substr($datefromdb,0,4);
$mon  = substr($datefromdb,4,2);
$day  = substr($datefromdb,6,2);
$hour = substr($datefromdb,8,2);
$min  = substr($datefromdb,10,2);
$sec  = substr($datefromdb,12,2);
$orgdate = date("l F dS, Y h:i A",mktime($hour,$min,$sec,$mon,$day,$year));
?>
Date: <? echo $orgdate; ?>

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.