Jump to content

[SOLVED] Quick and easy way to truncate a date string?


Moron

Recommended Posts

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

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.