tags:

views:

79

answers:

3

I'm working on a project that requires me to read values from a file and manipulate them slightly before storing them elsewhere. One of the things I need to do is convert some dates from mm/dd/yy format to mm/dd/yyyy format. Unfortunately for me, I am relatively new to PHP and regular expressions (which I assume is one of the better ways to solve this problem), and am therefore somewhat mystified. Any and all help will be appreciated. Thanks!

+1  A: 

You can try this, it may or may not work:

$new_date = date( 'm/d/Y', strtotime( $old_date ) );

Where $old_date is in the format you're talking about.

Kerry
Nice. Worked for me.
Peter Ajtai
+5  A: 

PHP has a built-in function strtotime() that is meant for just this kind of task... it'll even do the best-guess for the 2-digit year following this rule: the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. Once you have the date/time in the UNIXy format that PHP prefers, then you can print it out however you want with the date() function.

<?php
$str = '02/28/98';

// in PHP 5.1.0+, strtotime() returns false if it fails
// previous to PHP 5.1.0, you would compare with -1 instead of false
if (($timestamp = strtotime($str)) === false) {
    echo "Couldn't figure out the date ($str)!";
} else {
    echo "Reformatted date is " . date('m/d/Y', $timestamp);
}
?>

(I presume we're timezone-agnostic here, or that would add complications.)

ewall
+1 for the exhaustive answer.
kiamlaluno
don't you mean date('MM/DD/Y'....? OP wants to go from YY to YYYY ... one Y is the four digit year.
Peter Ajtai
and I mean 'm/d/Y' in the very last line (can't edit my previous comment). Lower case m and d keep the leading zeros.
Peter Ajtai
@Peter Ajtai -- yes, you're totally right. (I should have tried it to confirm before posting!) I'll correct it now.
ewall
+1  A: 

One of the problems here is that YY, assuming it is relatively current, can be either 19YY or 20YY. This means you should pick a number to be the cut off. Let's call this $cutOff If YY is less than $cutOff, we want 20YY if greater than we want 19YY

You could do it with regex, but since your example is simple and regex tends to be slower, you can simply use string manipulation with substr and substr_replace.

Here's how to change a string mm/dd/yy int mm/dd/yyyy:

<?php
// Our date
$str = "01/04/10";
$cutoff = 50;
// See what YY is
// Get the substring of $str starting two from the end (-2)... this is YY
$year = substr($str, -2);
// Check whether year added should be 19 or 20
if ($year < 50)
    // PHP converts string to number nicely, so this is our 20YY
    $year += 2000;
else
    // This is 19YY
    $year += 1900;
// Repace YY with YYYY
// This will take $str and replace the part two from the end (-2) undtil 
// the end with $year.
$str = substr_replace($str, $year, -2);  
// See what we got
echo $str;
?>
Peter Ajtai
Great explanation, and this tactic is quite necessary if `strtotimes()`'s use of "70" as the pivot year... or if you have dates *before* 1970, because they don't fit in the UNIX seconds-elapsed-since-the-epoch dates.
ewall