tags:

views:

49

answers:

3

I have a date/time string like this: 180510_112440 in this format ddmmyy_hhmmss I need a snippet for having a string formatted like this way: 2010-05-18 11:24:40 Thanks for help.

+1  A: 
$inDate = '180510_112440';
$date = strtotime('20'.substr($inDate,4,2).'-'.
        substr($inDate,2,2).'-'.
        substr($inDate,0,2).' '.
        substr($inDate,7,2).':'.
        substr($inDate,9,2).':'.
        substr($inDate,11,2));

echo date('d-M-Y H:i:s',$date);

Assumes date will always be in exactly the same format, and always 21st century

Mark Baker
+2  A: 

Hi, another possible answer is the common use of strptime to parse your date and the mktime function:

<?php

$orig_date = "180510_112440";

// Parse our date in order to retrieve in an array date's day, month, etc.
$parsed_date = strptime($orig_date, "%d%m%y_%H%M%S");

// Make a unix timestamp of this parsed date:
$nice_date = mktime($parsed_date['tm_hour'],
                    $parsed_date['tm_min'],
                    $parsed_date['tm_sec'],
                    $parsed_date['tm_mon'] + 1,
                    $parsed_date['tm_mday'],
                    $parsed_date['tm_year'] + 1900);

// Verify the conversion:
echo $orig_date . "\n";
echo date('d/m/y H:i:s', $nice_date);
Patrick MARIE
this is much better!
Kreker
Note: This function is not implemented on Windows platforms.
Kreker
+1  A: 
list($d,$m,$y,$h,$i,$s)=sscanf("180510_112440","%2c%2c%2c_%2c%2c%2c");
echo "20$y-$m-$d $h:$i:$s";
Col. Shrapnel