Forum Moderators: coopster

Message Too Old, No Replies

Exploding A TimeStamp

or any string without a dilimeter

         

theriddla1019

9:03 pm on Apr 29, 2004 (gmt 0)

10+ Year Member



Is there a way to explode a date string php variable hhmmssyyyyddmm etc. without any dilimeters. Im assuming its something like javascript {4,2} etc if there is one. Searched the site and couldnt find an article that exploded the string that didnt use dilimeters. Maybe a burried post or something i missed on php.net.
Help!
Adam :)

..
What the thing is..people will be entering in claims and they like 8 char dates since its faster 10192002
but i cant save that in mysql cause it will save it as Feb 20th 1019. I need to explode it reorganize it and save it as 20021019. and im used to doing that with - or / and they dun want that...

coopster

9:40 pm on Apr 29, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



One option is to build it into the format you want with a couple of PHP functions:

$date = '10192002'; 
echo date [php.net]("Y-m-d", mktime [php.net](0, 0, 0, substr [php.net]($date,0,2), substr($date, 2, 2), substr($date, 4, 4)));

Offers a format for MySQL date columns...

2002-10-19

Hmmmm...rethinking that, why even go through the date/time functions; just substring it together...

echo substr($date, 4, 4), '-', substr($date,0,2), '-', substr($date, 2, 2));

You could also use checkdate [php.net] to verify the entry by the operator.



Lastly, you could let MySQL do the concatenation for you, but you are at a bit of risk here as you haven't edit checked the data. And if you are going to edit check the data, you may as well assign it to a variable as opposed to using something like this...
INSERT INTO mytable (date) VALUES(concat(substring('10192002',5,4),'-',substring('10192002',1,2),'-',substring('10192002',3,2))) ;

Netizen

10:48 pm on Apr 29, 2004 (gmt 0)

10+ Year Member



Or

$date = '10192002';

$reformattedDate=preg_replace('/(\d{2})(\d{2})(\d{4})/',"$3-$1-$2",$date);

would give '2002-10-19';

(Untested code but should work)

theriddla1019

2:06 pm on Apr 30, 2004 (gmt 0)

10+ Year Member



Works like a charm. Thanks everyone :)