Jump to content


Photo

Date Format Help


  • Please log in to reply
7 replies to this topic

#1 paulspoon

paulspoon
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 13 July 2006 - 08:46 AM

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


#2 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 13 July 2006 - 08:50 AM

post some code on how you got the date please

#3 CheesierAngel

CheesierAngel
  • Members
  • PipPipPip
  • Advanced Member
  • 105 posts
  • LocationBelgium

Posted 13 July 2006 - 08:53 AM

I suppose the date is not generated but inputted by the costumers. You could use:

<?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;
?>

Or you could use following aswell:
<?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;
?>


#4 paulspoon

paulspoon
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 13 July 2006 - 09:21 AM

Doesn't work ?

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

please help

#5 CheesierAngel

CheesierAngel
  • Members
  • PipPipPip
  • Advanced Member
  • 105 posts
  • LocationBelgium

Posted 13 July 2006 - 09:25 AM

You can change it to

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


#6 paulspoon

paulspoon
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 13 July 2006 - 09:35 AM

Still nothing , next error

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

Sorry for the inconvinience

#7 CheesierAngel

CheesierAngel
  • Members
  • PipPipPip
  • Advanced Member
  • 105 posts
  • LocationBelgium

Posted 13 July 2006 - 09:40 AM

Just tested it and is working:

<?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;
?>

Also working:
<?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;
?>

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

#8 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 13 July 2006 - 09:42 AM

$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; ?>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users