0

Possible Duplicate:
Convert one date format into another in PHP

I have a user formatted date and the correspondant format using PHP date format (http://fr.php.net/manual/en/function.date.php). This format is not known before user input.

I have to convert this date to a known format like 'd/m/Y h:i:s'. My PHP server uses the 5.2.1 PHP version so I can't use DateTime class.

Someone has an alternative to DateTime::createFromFormat method, a script doing like this or an idea to help me ?

Best regards, Florian.

Edit :

I write a function which works to convert a date to an other format without to know original date format :

/**
 * This function converts a date with unknown format into a defined format.
 * It could detect some standard date formats : dd/mm/YYYY, dd-mm-YY, YYYY-mm-dd hh:mm:ss, etc.
 * @param string $date date which is user formatted.
 * @param int $options used to help algorithm to detect date format : US or FR date, past date or future date, hour format.
 * @param string $newFormat format the date must be converted into
 * @param string $originalFormat format detected
 * @return boolean|string the new date, corresponding to the $date argument, formatted into the $newFormat format
 */
function convertDateToFormat($date, $options = 0, $newFormat, &$originalFormat = ''){
    if($options == 0){
        $options = DATE_FR | DATE_PAST | HOUR_24;
    }
    $matches = array();

    $year = 0;
    $month = 0;
    $day = 0;
    $hour = 0;
    $minute = 0;
    $second = 0;

    $originalFormatOk = false;

    if(preg_match('#(\d{1,2})([/.-])(\d{1,2})(([/.-])(\d{2,4}))?#', $date, $matches)){
        if(isset($matches[6])){
            if(strlen($matches[6]) == 2){
                if($matches[6] >= date('y') && DATE_PAST)
                    $year = '19'.$matches[6];
                else
                    $year = '20'.$matches[6];
                $originalFormatyear = 'y';
                if($options & DATE_US){
                    $originalFormat = 'm'.$matches[2].'d'.$matches[5].$originalFormatyear;
                    $year = $matches[6];
                    $month = $matches[1];
                    $day = $matches[3];
                    $originalFormatOk = true;
                }
            }
            else{
                $year = $matches[6];
                $originalFormatyear = 'Y';
            }
            if(!$originalFormatOk){
                $year = $matches[6];
                $month = $matches[3];
                $day = $matches[1];
                $originalFormat = 'd'.$matches[2].'m'.$matches[5].$originalFormatyear;
                $originalFormatOk = true;
            }
        }
        else{
            if($options & DATE_US){
                $originalFormat = 'm'.$matches[2].'d';
                $year = date('Y');
                $month = $matches[1];
                $day = $matches[3];
            }
            else{
                $originalFormat = 'd'.$matches[2].'m';
                $year = date('Y');
                $month = $matches[3];
                $day = $matches[1];
            }
            $originalFormatOk = true;
        }

    }

    if(preg_match('#'.$matches[0].'(\D*)(\d{1,2})([:hH])(\d{1,2})(:(\d{1,2}))?#', $dateHour, $matches)){
        if($options & HOUR_12)
            $originalFormatHour = 'h';
        else
            $originalFormatHour = 'H';
        $hour = $matches[2];
        $minute = $matches[4];
        if(strtolower($matches[3]) == 'h')
            $matches[3] = '\\'.$matches[3];
        $originalFormat .= $matches[1].$originalFormatHour.$matches[3].'i';
        if(isset($matches[6])){
            $second = $matches[6];
            $originalFormat .= ':s';
        }
    }

    if($originalFormatOk)
        return date($newFormat, mktime($hour, $minute, $second, $month, $day, $year));
    return false;
}
Community
  • 1
  • 1
flchaux
  • 100
  • 1
  • 13
  • Not exactly sure what you mean, but in php you can use strtotime function to try to decipher the input, i.e. date('d/m/Y h:i:s', strtotime('yesterday')); – i-- Aug 13 '12 at 14:58
  • Can you give us an example of the date you want to convert ? – Anas Aug 13 '12 at 14:59
  • Why can't you simply use `$tstamp = strtotime($userdate)` then `date('d/m/Y h:i:s', $tstamp)`? – user1027562 Aug 13 '12 at 15:01
  • For @therao: the date is submitted in a known format and needs to be decoded into a common format. So if user submits 10/11/2012 and they are UK convert to MySQL 2012/11/10 as format is dd/mm/YYYY. But if US convert to 2012/10/11 as format is mm/dd/YYYY? – Waygood Aug 13 '12 at 15:02
  • If a date is submitted in a format other than US then strtotime() will not work __correctly__ e.g. US dates are mm/dd/yyyy where as UK are dd/mm/yyyy. If you use MySQL format yyyy-mm-dd hh:ii:ss then it will be easier to deal with as strtotime(), comparisons and database storage will be easier. – Waygood Aug 13 '12 at 15:15
  • I write a function which works to convert a date to an other format without to know original date format : – flchaux Oct 02 '13 at 13:00

1 Answers1

2

The date_parse_from_format() function does everything you need.

E.g. (from the docs):

$date = "6.1.2009 13:00+01:00";
print_r(date_parse_from_format("j.n.Y H:iP", $date));

However, if you do not know the format it's more or less a rough guess and you'd have to use something like date_parse() which gives you (hopefully) a correct date but may horrible fail.

E.g. (from the docs):

print_r(date_parse("2006-12-12 10:00:00.5"));

Will print:

Array
(
    [year] => 2006
    [month] => 12
    [day] => 12
    [hour] => 10
    [minute] => 0
    [second] => 0
    [fraction] => 0.5
    [warning_count] => 0
    [warnings] => Array()
    [error_count] => 0
    [errors] => Array()
    [is_localtime] => 
)
fdomig
  • 4,417
  • 3
  • 26
  • 39