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.
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.
// 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; }
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.
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).
$sale_date_year = substr ($sale_date, -4, 4);
So at this point $sale_date_year = 1982.
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).
$sale_date_day = substr ($sale_date, -6, 2);
This function will return $sale_date_day = 29.
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.
$string_to_replace = $sale_date_day . $sale_date_year;
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 -
$sale_date_month = str_replace($string_to_replace,'',$sale_date);
This will return $sale_date_month = 3
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.
$sale_date_clean = "$sale_date_month/$sale_date_day/$sale_date_year";
The $sale_date_clean will now look like this – 3/29/1982.
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.
$unix_date = strtotime($sale_date_clean);
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.
Posted on September 30, 2009 in PHP Tutorials and has been tagged as dates, php.
No Comments
No comments yet.
RSS feed for comments on this post. TrackBack URL
My name is Andy Weigel and I'm a web developer and designer in Pittsburgh, PA.
I focus on combining design with technology to build compelling, creative, easy-to-navigate web sites and custom web applications for organizations and businesses of all shapes and sizes. My specialty is WordPress. And most of all, I love what I do!