Forum Moderators: coopster

Message Too Old, No Replies

Date conversion

I need to change the format of date stamp

         

sneaks

1:57 am on Aug 10, 2005 (gmt 0)

10+ Year Member



need to convert a date in this format:
20050808

to something a little more human such as

August 8, 2005

brendan3eb

4:04 am on Aug 10, 2005 (gmt 0)

10+ Year Member



You can try using the date() function - [us2.php.net...]

However that doesn't look like a time() timestamp. I'm not familiar with what kind of timestamp that is. You could just split it up like this:

<?php
$stamp = "20050808";
$indchars = preg_split('//', $stamp, -1, PREG_SPLIT_NO_EMPTY);
$k=0;
while($k < 4)
{
$year = $year.$indchars[$k];
$k++;
}
while($k < 6)
{
if($indchars[$k] == 0)
{
$indchars[$k] = "";
}
$month = $month.$indchars[$k];
$k++;
}
while($k < 8)
{
if($indchars[$k] == 0)
{
$indchars[$k] = "";
}
$day = $day.$indchars[$k];
$k++;
}
$monthlist = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$month_name = $monthlist[$month];
echo "$month_name $day, $year";
?>

sneaks

5:05 am on Aug 10, 2005 (gmt 0)

10+ Year Member



to me this seems a little extraneous but it works....


$newsDate=str_split(($nrEntry->nrDate), 4);
$nrYear=intval($newsDate[0]);
$spltAgain=str_split($newsDate[1], 2);
$nrMonth=intval($spltAgain[0]);
$nrDay=intval($spltAgain[1]);
date("l, F j, Y.", mktime(0,0,0, $nrMonth, $nrDay, $nrYear))

i broke up the string: yyyymmdd and convert to integers

hope this helps someone else by the way,
is there a way to search the forums?

j.

stecostello

3:10 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



wouldnt
date("F", "j", "Y")
be simpler?

sneaks

4:44 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



i am not following waht your suggesting because i dont want the current date, i want to convert a string (a custom timestamp in the format: "YYYYMMDD") and convert to a humanized date...

if you know a better way i am listening :)

j

jatar_k

5:22 pm on Aug 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



because of the format you had it in originally your way is the best, if you had - or / in it you could just explode and use the pieces in a mktime/strftime or some such.

mattx17

5:33 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



How about:

$TimeStamp = "20050808"; 
preg_match("#^([0-9]{4})([0-9]{2})([0-9]{2})#",$TimeStamp,$Match);
list(,$Year,$Month,$Day) = $Match;
print(date("F jS, Y",mktime(0,0,0,$Month,$Day,$Year)));

sneaks

7:20 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



slick!
thx...
j.