Moron Posted September 4, 2007 Share Posted September 4, 2007 Lets say that I have a date string in an older database where March 19th, 2004 is expressed as: 3192005 How can I truncate this to give me: Month: 3 Day: 19 Year: 2004 Thanks! Quote Link to comment Share on other sites More sharing options...
sasa Posted September 4, 2007 Share Posted September 4, 2007 what date is string '1112005'? Quote Link to comment Share on other sites More sharing options...
hostfreak Posted September 4, 2007 Share Posted September 4, 2007 Probably a better way, but here is what I came up with: <?php $date = '3192005'; if (strlen($date) > '7') { //month with 2 digits $month = substr("$date", 0, 2); $day = substr("$date", 2, 2); $year = substr("$date", 4, 4); } else { //month with single digit $month = substr("$date", 0, 1); $day = substr("$date", 1, 2); $year = substr("$date", 3, 4); } echo "Month: $month<br />Day: $day<br />Year: $year"; ?> Quote Link to comment Share on other sites More sharing options...
Moron Posted September 4, 2007 Author Share Posted September 4, 2007 what date is string '1112005'? That would be January 11, 2005. Trust me, the date format wasn't my decision! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 4, 2007 Share Posted September 4, 2007 you need to be consistent '01112007' would be fine but '1112007' COULD be 1st of november too!!! if you are consitent then hostfreaks soln without the if statement will do the job. Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 4, 2007 Share Posted September 4, 2007 what date is string '1112005'? That would be January 11, 2005. Trust me, the date format wasn't my decision! So what date string would represent November 1, 2005? I think there needs to be some serious revision to the dates stored in order for the 'conversion' to be of real value. Quote Link to comment Share on other sites More sharing options...
hostfreak Posted September 4, 2007 Share Posted September 4, 2007 Ah, didn't even take into account a one digit days in my solution. You definitely need some consistency... unless you want it to get long and complicated. Quote Link to comment Share on other sites More sharing options...
Moron Posted September 4, 2007 Author Share Posted September 4, 2007 So what date string would represent November 1, 2005? 11012005. It's Month (one or two digits, as applicable), 2-digit Day, 4-digit Year. Yeah, it's pretty retarded but that's what I have to work with. I'm thinking that whatever technique I use, I'll need to count from the right, since the day and year are consistent. Quote Link to comment Share on other sites More sharing options...
hostfreak Posted September 4, 2007 Share Posted September 4, 2007 My solution should work fine then, if single digit days are preceded with 0. Quote Link to comment Share on other sites More sharing options...
Moron Posted September 5, 2007 Author Share Posted September 5, 2007 My solution should work fine then, if single digit days are preceded with 0. It works. Thanks! Quote Link to comment Share on other sites More sharing options...
hostfreak Posted September 6, 2007 Share Posted September 6, 2007 Sorry to bring this back up, but an even better way: <?php $date = '3192005'; $month = (strlen($date) > 7) ? substr("$date", 0, 2) : substr("$date", 0, 1); $day = substr("$date", -6, 2); $year = substr("$date", -4, 4); echo "Month: $month<br />Day: $day<br />Year: $year"; ?> Less code Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.