PHP date reformat function I am currently working on an app and have some issues with the date handling.
Found a simple solution, and want your opinion.
First thing, that I want to achieve:


I have 2 date format inputs, one is like 2013-09-30 14:20:00 and the other is 30/09/2013 1420
Time stamp is given with no day light saving time.


So, I have to store my date/time in DB with the same format in order to compare one with each other, and also I need to adjust the time stamp +1 hour if it is in the DST period.
For this, I made the following function:

function format_date($date_string, $date_format)
{
$unix = mktime( substr($date_string, strpos($date_format, 'HH'), 2),
                (strpos($date_format, 'ii'))? substr($date_string, strpos($date_format, 'ii'), 2) : '00',
                (strpos($date_format, 'ss'))? substr($date_string, strpos($date_format, 'ss'), 2) : '00',
                substr($date_string, strpos($date_format, 'MM'), 2),
                substr($date_string, strpos($date_format, 'DD'), 2),
                substr($date_string, strpos($date_format, 'YYYY'), 4)
            ) ;
$human = date('Y-m-d H:i:s', $unix);
$dst = date('I', $unix);

return (object)array(
                'unix' =&gt; $unix,
                'human' =&gt; $human,
                'dst' =&gt; $dst
                );
}

//Usage:
$date1 = format_date('2013-09-30 14:20','YYYY-MM-DD HH:ii');
$date2 = format_date('30/09/2013 1420','DD/MM/YYYY HHii');

echo $date1-&gt;human ;
echo '&lt;br /&gt;';
echo $date2-&gt;human ;
echo '&lt;br /&gt;';
//  $date1-&gt;human is the same with $date1-&gt;human, also -&gt;unix


Maybe this will help others, but if you find something not right in this function please let me know before I put it in production.

Update: (thank you Glavić)

function format_date1($date_string, $date_format)
{
$date_build = DateTime::createFromFormat ( $date_format , $date_string  );
return (object)array(
                'unix' =&gt; $date_build-&gt;format('U'),
                'human' =&gt; $date_build-&gt;format('Y-m-d H:i:s'),
                'dst' =&gt; $date_build-&gt;format('I')
                );
}
$make_date = format_date1('30/09/2013 1420', 'd/m/Y Hi');
print_r($make_date); 
//returns stdClass Object ( [unix] =&gt; 1380540000 [human] =&gt; 2013-09-30 14:20:00 [dst] =&gt; 1 )
//Use: $make_date-&gt;unix | $make_date-&gt;human | $make_date-&gt;dst
Use DateTime::createFromFormat method to convert datetime strings to DateTime object:

$date1 = DateTime::createFromFormat('Y-m-d H:i', '2013-09-30 14:20');
$date2 = DateTime::createFromFormat('d/m/Y Hi', '30/09/2013 1420');


Now you can just format your output:

echo 'unix  : ' . $date1-&gt;format('U') . "\n";
echo 'human : ' . $date1-&gt;format('Y-m-d H:i:s') . "\n";
echo 'dst   : ' . $date1-&gt;format('I') . "\n";


And you don't need to adjust timestamp for +1 hour if in DST, DateTime can handle leap years and DST correctly. Set timezone when you create DateTime object, or change timezone after.