Convert a String into a Date with PHP

PHP Tutorials

This problem came up while working with the Allegheny County Property database. They have a field for the sale date of the property, only they’re storing the date like this – 3291982 for 03/29/1982.

The problem is that there is no leading zero for the month, so using substr function and counting 2, 2 and 4 won’t work in every situation. Here’s what I did to create a human readable date (03/29/1982) as well as a UNIX time variable.

The Functions

For this conversion I’m going to use three functions – substr, str_replace and strtotime. Here’s a quick breakdown of how they work.

substr ( string $string , int $start [, int $length ] ) – this returns a portion of the string based on the start and length parameters. You can use negative numbers for the start to pull from the end of the string, which is what we’ll be doing.

str_replace ( mixed $search , mixed $replace , mixed $subject) – this function will return a string or an array with all occurences of the search parameter in the subject parameter replaced with the replace parameter.

strtotime ( string $date ) – this function takes a US English date format and will parse it into a UNIX timestamp.

Down to Business

{code type=php}
// Takes a date (11222009) and returns 11/22/2009
function fix_date($sale_date)
{
$sale_date_year = substr ($sale_date, -4);
$sale_date_day = substr ($sale_date, -6, 2);

$string_to_replace = $sale_date_day . $sale_date_year;

$sale_date_month = str_replace($string_to_replace,”,$sale_date);

$sale_date_clean = “$sale_date_month/$sale_date_day/$sale_date_year”;

if ($sale_date_clean == “//”) {$sale_date_clean = NULL;}

return $sale_date_clean;

}
{/code}

I pulled the sale date from the database and created the $sale_date variable out of it. For this example lets suppose that $sale_date = 3291982.

Let’s Breakout the Year

Next, I figured that the last 4 digits will always be the year. I was able to create a variable from them using the substr function that’s provided by PHP. What this function is doing is telling it to take the $sale_date and take the last 4 numbers off the end (-4)for a total of 4 numbers (4).

{code type=PHP}
$sale_date_year = substr ($sale_date, -4, 4);
{/code}

So at this point $sale_date_year = 1982.

Getting the Day of the Month

The next step was to strip out the day of the month using the same substr function – this time telling it to take 6 numbers from the end (-6) for a total of 2 numbers (2).

{code type=PHP}
$sale_date_day = substr ($sale_date, -6, 2);
{/code}

This function will return $sale_date_day = 29.

Now for the Tricky Part…

I’m going to combine the 2 new variables together to create a string to use as my replace variable to use the str_replace function.

{code type=PHP}
$string_to_replace = $sale_date_day . $sale_date_year;
{/code}

At this point $string_to_replace = 291982. I’m going to use this in the str_replace function to get the month from the $sale_date variable.Here’s the function –

{code type=PHP}
$sale_date_month = str_replace($string_to_replace,”,$sale_date);
{/code}

This will return $sale_date_month = 3

Creating a User-Friendly Date

The next step is to create a human readable date. To this, I simply combine all the separate variables into one variable, separated with a slash.

{code type=PHP}
$sale_date_clean = “$sale_date_month/$sale_date_day/$sale_date_year”;
{/code}

The $sale_date_clean will now look like this – 3/29/1982.

Make the Pretty Date into a UNIX Timestamp

The final step will be to take the $sale_date_clean that we just created and make it into a UNIX timestamp. We’ll use the strtotime function of this.

{code type=PHP}
$unix_date = strtotime($sale_date_clean);
{/code}

At this point we have 2 clean variables to do whatever you want with.

Hopefully you’ve found this quick tutorial valuable. Contact me if you have any questions or would do it a different way. There’s 1,001 ways to do everything – that’s why I love coding.

Leave a Reply

Your email address will not be published. Required fields are marked *

What's the Buzz?

Andy is a true top notch programmer with a great eye for design, style and balance. When I out source web development projects, I look for qualified and competent programmers that can understand the big picture, add value throughout entire process, and pays close attention to the details – Andy fits the bill.

Andy is well-versed in web development using various programming languages and CMS platforms. Andy is able to deliver a flawless website based on the creative vision and project requirements. He is able to do it on time and on budget.

I highly recommended Andy as a web developer and give him four stars on his professionalism.

- John LeDonne
LeDonne Creative