Hello All.
I have a varible containing a date, which I need to convert.
Convert 19730405 To: 04/05/1973
Anybody have suggestions?
Thanks.
| SitePoint Sponsor |


Hello All.
I have a varible containing a date, which I need to convert.
Convert 19730405 To: 04/05/1973
Anybody have suggestions?
Thanks.





you can use the following function:PHP Code:<?
function timestamp_convert($timestamp)
{
$year = substr($timestamp, 0, 4);
$month = substr($timestamp, 4, 2);
$day = substr($timestamp, 6, 2);
$new_date = $month."/".$day."/".$year;
return $new_date;
}
?>
Last edited by Defender1; Dec 14, 2001 at 05:22.
Assuming that this is a fixed length string (8 chars):
I have assumed the American standard date format (M/d/Y)PHP Code:<?php
$dateStr = '19730405';
$year = substr($dateStr, 0, 4);
$month = substr($dateStr, 4, 2);
$day = substr($dateStr, 6, 2);
$formattedDateStr = "$month/$day/$year";
?>


Thank You Guys!
You guys got the brain-juices flowing again
talk about programmers-block... wow, the mind just froze on this one!
Anyway, here is what I worked.
It checks for pre 1000 year dates too.
PHP Code:<?php
function timestamp_convert($str_input)
{
$year = substr($str_input, 0, 4);
$month = substr($str_input, 4, 2);
$day = substr($str_input, 6, 2);
////////////////////////////////////////
// DETECT ZERO STARTING YEAR
////////////////////////////////////////
$year1 = substr($year, 0, 1);
if ($year1 == "0") {
$year = substr($str_input, 1, 3);
} else {
$year = substr($str_input, 0, 4);
}
////////////////////////////////////////
//
$new_date = $month."/".$day."/".$year;
return $new_date;
}
print timestamp_convert("09090414");
?>
Again, thanks guys.
Last edited by AbelaJohnB; Dec 14, 2001 at 05:45.
BTW, there is a known bug with the PHP tags that has crept into the forums, with the upgrades that Wayne has recently introduced.
To get around it make sure you include the opening and closing <?php and ?> tags in your code when you post!
![]()





Uh.. Am I missing something here, or are you guys making this way more difficult than it needs to be.
PHP Code:<?php
$date = 19730405;
$date = date("m/d/Y", strtotime($date));
?>
Please don't PM me with questions.
Use the forums, that is what they are here for.
Bookmarks